I am new to OOPS
I want to know when a Parent Class instance P is initialized and I want to initialize a child class instance C
like
class A(object):
def __init__(self):
print "A created"
P = A()
class B(A):
def __init__(self):
print super((B), self).__init__ # Q1: How to get "A created" from parent class?
print self.__class__.__name__
print super(B, self).__class__.__name__
Q2: Now if I create an instance z = B(), I want to create z as a sub-instance of P i.e. z should only be created if an earlier instance P exists.
And the attributes of P should flow down to z w/ the data if any added to attributes.
Basically z should not be created if P is not present
similarly y = B() iff x = A()
Is there a way to do that?
I am doing this because I am creating a program where multiple instance would be created of the Class A in various context, say
Mathematics = A()
English = A()
Now,
Algebra = B() iff Mathematics = A()
And
Grammar = B() iff English = A()
Is there a self checker to check that? I mean how is that done?
UPDATE on Q2: Another way of asking that is, is there a way to check in B class while creating an instance to check for instances of A class and getting a particular instances data? Like in B class checking the instances made of Class A and then selecting one and getting data from that instance's attributes to the instance being created of Class B??
Also
z = B()
<bound method B.__init__ of <__main__.B object at 0x000000000791DB38>>
B
super #Q3: I thought it would print `A`. WHY NOT?
__init__in theB.__init__()- you need brackets to call a function.test, and I dotestin Python, I just get that function object. If I dotest()(potentially with arguments), I actually call the function.x.testcan result in a function being called if you use the@propertydecorator on thetestfunction or havetest = property(...)in the body of the class. Of course, only mean people write getters that have side effects...