Imagine this:
class A(object): class B(object): def __init__(self): super(B, self).__init__()
This creates an error:
NameError: global name B is not defined.
I've tried A.B
, but then it says that A
is not defined.
Update:
I've found the problem.
I've had a class like this:
class A(object): class B(object): def __init__(self): super(B, self).__init__() someattribute = B()
In that scope, A isn't defined yet.
The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super.
The “__init__” is a reserved method in python classes. It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. Python super() The super() function allows us to avoid using the base class name explicitly.
I'm not sure why A.B is not working correctly for you, as it should.. Here's some shell output that works:
>>> class A(object): ... class B(object): ... def __init__(self): ... super(A.B, self).__init__() ... def getB(self): ... return A.B() ... >>> A().getB() <__main__.B object at 0x100496410>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With