Static inner class in python

My code must have an inner class, and I want to instantiate this inner class without instantiating the outer class.
How to do this in python? In java, we can determine that the inner class is static, but I don't know how to make the inner class static in python. I know that for methods we can use the @staticmethod decorator.

 class Outer: def __init__(self): print 'Instance of outer class is created' class Inner: def __init__(self): print 'Instance of Inner class is created' 
+6
source share
3 answers

The Inner class is defined during the definition of the Outer class and it exists in its class namespace. So just Outer.Inner() .

+6
source

You do not need to do anything special. Just contact him directly:

 instance = Outer.Inner() 
+1
source

Not sure if this is what you are looking for. You can simply create it as a class variable or a global (modular) variable

 class Outer: def __init__(self): print 'Instance of outer class is created' class Inner: def __init__(self): print 'Instance of Inner class is created' i = Inner() # as class varaible i1 = Outer.Inner() # as module varaible 
0
source

Source: https://habr.com/ru/post/950459/


All Articles