Overriding __init__() questions

grocery_stocker cdalten at gmail.com
Sat Apr 11 17:06:12 EDT 2009


What's the difference between doing something calling A.__init__(self)
like in the following...

[cdalten at localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...    def __init__(self):
...       pass
...
>>> class B(A):
...    def __init__(self, x):
...        A.__init__(self)
...        self.x = x
...        print x
...
>>> x=B(5)
5
>>> y=A(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (2 given)
>>> y = A()
>>>

versus something like the following....

[cdalten at localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...    def __init__(self):
...       pass
...
>>> class B(A):
...    def __init__(self, x):
...        self.x = x
...        print x
...
>>> x = B(5)
5
>>> y = A(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (2 given)
>>> y = A()
>>>

Just curious because the former seems to be common when using the
python Thread module.



More information about the Python-list mailing list