Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static inner class in python

My code needs to have an inner class and I want to create the instance of this inner class without creating the instance of outer class.
How to do so in python? In java we can define the inner class to be static but I don't know how to make a inner class static in python. I know that for methods we can use @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'
like image 456
Abhishek Gupta Avatar asked Jul 28 '13 09:07

Abhishek Gupta


2 Answers

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

like image 179
user87690 Avatar answered Nov 04 '22 00:11

user87690


You don't need to do anything special. Just refer to it directly:

instance = Outer.Inner()
like image 1
Martijn Pieters Avatar answered Nov 04 '22 00:11

Martijn Pieters