newb __init__ inheritance
Hello everyone. This is my first post in this group. I started learning python a week ago from the "dive into python" e- book and thus far all was clear. However today while reading chapter 5 about objects and object orientation I ran into something that confused me. it says here: http://www.diveintopython.net/object_oriented_framework/defining_classes.html#fileinfo.class.example "__init__ methods are optional, but when you define one, you must remember to explicitly call the ancestor's __init__ method (if it defines one). This is more generally true: whenever a descendant wants to extend the behavior of the ancestor, the descendant method must explicitly call the ancestor method at the proper time, with the proper arguments. " However later on in the chapter: http://www.diveintopython.net/object_oriented_framework/userdict.html it says: "Methods are defined solely by their name, and there can be only one method per class with a given name. So if a descendant class has an __init__ method, it always overrides the ancestor __init__ method, even if the descendant defines it with a different argument list. And the same rule applies to any other method. " My question is if __init__ in the descendant class overrides __init__ in the parent class how can I call the parent's __init__ from the descendant class - I just overrode it didn't I? Am I missing something more fundamental here? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: newb __init__ inheritance
On Mar 11, 12:47 am, "Colin J. Williams" wrote:
> On 10/03/2012 12:58 PM, Colin J. Williams wrote:> On 08/03/2012 10:25 AM,
> hyperboogie wrote:
> >> Hello everyone.
>
> [snip]
> > main()
> > I'm not sure that the class initialization is required.
>
> > Good luck,
>
> > Colin W.
>
> When I wrote earlier, I wondered about the need for initialization.
>
> With Version 2, both __new__ and __init__ were required, not in the
> example below, using version 3.2:
> #!/usr/bin/env python
>
> class A():
>
> def ringA(self):
> print ('aaa')
>
> def ringB(self):
> print('bbb')
>
> class B(A):
> def __init__(self:)
> def ringB(self):
> print('BBB')
>
> a= A()
> b= B()
> b.ringB()
> b.ringA()
> b.__class__.mro()[0].ringB(22) # 22 is used for the ringB attribute
> # Trial and error shows that any
> # non-Null,including None for the
> # argument gives the same result
> z= 1
> def main():
> pass
>
> if __name__ == '__main__':
> main()
>
> Colin W.
thank you everyone...
Still things are not working as expected... what am I doing wrong?
I'm working with python2 and have the following issues:
1. mro is not an attribute/function
2. inheritance is not working as expected:
# cat test.py
#!/usr/bin/python
class A():
def __init__(self):
z=1
print "in A.__init__ z=", z
def funcA(self):
print "in funcA - class A"
def funcB(self):
print "in funcB - class A, z= ", z
class B(A):
def __init__(self):
A.__init__(self)
print "in B.__init__ z=", z
def funcB(self):
print "in funcB - class B, z= ", z
a=A()
b=B()
b.funcB()
b.funcA()
# ./test.py
in A.__init__ z= 1# This must be the __init__ from the
instantiation of a
in A.__init__ z= 1# This must be the B.__init__ calling A.__init__
in B.__init__ z= # Why isn't this working? z should have been
inherited from "A" right?
Traceback (most recent call last):
File "./test.py", line 23, in
b=B()
File "./test.py", line 17, in __init__
print "in B.__init__ z=", z
NameError: global name 'z' is not defined
#
--
http://mail.python.org/mailman/listinfo/python-list
Re: newb __init__ inheritance
On Sunday, March 11, 2012 12:38:27 PM UTC+2, Chris Rebert wrote: > On Sun, Mar 11, 2012 at 3:18 AM, hyperboogie wrote: > > > thank you everyone... > > Still things are not working as expected... what am I doing wrong? > > > # cat test.py > > #!/usr/bin/python > > > > class A(): > > You should be subclassing `object`, but that's a minor point which > isn't the cause of your problem. > > > def __init__(self): > > z=1 > > This creates a *local variable* named "z". You want an *attribute* > named "z", so you should be doing: > self.z = 1 > instead. Same problem elsewhere; you must *always* explicitly use > `self` when referencing an attribute of the current object. Python != > Java or C++. > > Cheers, > Chris Thanks ... works great now. Two last questions: 1. What do you mean by "subclassing `object`"? 2. Is the mro function available only on python3? -- http://mail.python.org/mailman/listinfo/python-list
Re: newb __init__ inheritance
On Thursday, March 8, 2012 5:25:06 PM UTC+2, hyperboogie wrote: > Hello everyone. > > This is my first post in this group. > I started learning python a week ago from the "dive into python" e- > book and thus far all was clear. > However today while reading chapter 5 about objects and object > orientation I ran into something that confused me. > it says here: > http://www.diveintopython.net/object_oriented_framework/defining_classes.html#fileinfo.class.example > > "__init__ methods are optional, but when you define one, you must > remember to explicitly call the ancestor's __init__ method (if it > defines one). This is more generally true: whenever a descendant wants > to extend the behavior of the ancestor, the descendant method must > explicitly call the ancestor method at the proper time, with the > proper arguments. " > > However later on in the chapter: > http://www.diveintopython.net/object_oriented_framework/userdict.html > > it says: > "Methods are defined solely by their name, and there can be only one > method per class with a given name. So if a descendant class has an > __init__ method, it always overrides the ancestor __init__ method, > even if the descendant defines it with a different argument list. And > the same rule applies to any other method. " > > My question is if __init__ in the descendant class overrides __init__ > in the parent class how can I call the parent's __init__ from the > descendant class - I just overrode it didn't I? > > Am I missing something more fundamental here? > Thanks Thank you so much everyone for you help. No doubt I still have a long way to go before I feel comfortable with python. Appreciate all your help... -- http://mail.python.org/mailman/listinfo/python-list
