How to refer to a child class from a parent method?
python
Solution
What is wrong with just using `self.x`?
class Parent(object):
x = None # default value
def __init__(self):
print self.x
class someChild(Parent):
x = 10
def __init__(self):
Parent.__init__(self)
class otherChild(Parent):
x = 20
def __init__(self):
Parent.__init__(self)
a = someChild()
# output: 10
b = otherChild()
# output: 20
Note how this works even if `Parent` has a class attribute `x` as well (`None` in the above example)- the child's takes precedence.
Problem
In the following sample, is there a magic word I can put in place of `<ChildClass>` that works like the opposite of super? ``` class Parent(object): def __init__(self): print <ChildClass>.x class someChild(Parent): x = 10 ``` It is a stupid example, but it shows my intention. By the way, using `someChild` will not work, because there are many child classes. The only solution I can think of is to have a constructor in every child class that calls the constructor of Parent with a reference to itself (or even to pass x), but I would like to avoid having a constructor at all in each child.