Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21391
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'new,': 0.04; 'method,': 0.05; 'defines': 0.07; 'python"': 0.07; 'python': 0.08; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'orientation': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subclass': 0.09; '~ethan~': 0.09; 'def': 0.13; 'argument': 0.15; 'method.': 0.15; '__init__': 0.16; 'b()': 0.16; 'clear.': 0.16; 'overrides': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'url:diveintopython': 0.16; 'cc:addr:python-list': 0.16; 'question.': 0.16; 'wrote:': 0.18; 'later': 0.19; 'thus': 0.21; 'cc:no real name:2**0': 0.21; 'header :In-Reply-To:1': 0.22; 'here?': 0.23; 'defined': 0.24; 'creating': 0.25; 'cc:2**0': 0.26; 'looks': 0.27; 'says': 0.27; 'explicitly': 0.29; 'class': 0.29; 'example': 0.29; 'cc:addr:python.org': 0.29; 'generally': 0.30; 'arguments.': 0.30; 'confused': 0.30; 'parent': 0.30; 'ran': 0.30; "didn't": 0.30; 'ago': 0.31; 'creates': 0.31; 'thanks': 0.32; 'does': 0.32; 'objects': 0.32; 'there': 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'object': 0.33; 'test': 0.34; 'chapter': 0.34; 'rule': 0.34; 'fundamental': 0.34; 'however,': 0.35; 'something': 0.35; 'list.': 0.35; 'post': 0.36; 'extend': 0.36; 'question': 0.36; 'class.': 0.37; 'everyone.': 0.37; 'but': 0.37; 'skip:_ 10': 0.38; 'uses': 0.38; 'missing': 0.40; 'one,': 0.40; 'more': 0.61; 'stop': 0.61; 'received:websitewelcome.com': 0.64; 'here:': 0.67; 'today': 0.70; 'chapter:': 0.84; 'one).': 0.84; 'url:example': 0.84; 'value):': 0.84 |
| Date | Thu, 08 Mar 2012 09:34:20 -0800 |
| From | Ethan Furman <ethan@stoneleaf.us> |
| User-Agent | Thunderbird 1.5.0.10 (Windows/20070221) |
| MIME-Version | 1.0 |
| To | hyperboogie <hyperboogie@gmail.com> |
| Subject | Re: newb __init__ inheritance |
| References | <1c6db866-6fa3-4de5-96de-51d6720a1300@x17g2000yqj.googlegroups.com> |
| In-Reply-To | <1c6db866-6fa3-4de5-96de-51d6720a1300@x17g2000yqj.googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - gator410.hostgator.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - stoneleaf.us |
| X-BWhitelist | no |
| X-Source | |
| X-Source-Args | |
| X-Source-Dir | |
| X-Source-Sender | mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:2443 |
| X-Source-Auth | ethan+stoneleaf.us |
| X-Email-Count | 14 |
| X-Source-Cap | dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.515.1331229151.3037.python-list@python.org> (permalink) |
| Lines | 78 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1331229151 news.xs4all.nl 6984 [2001:888:2000:d::a6]:35704 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:21391 |
Show key headers only | View raw
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
An excellent question.
What you subclass you are creating a new, different class.
class A(object):
def __init__(self):
print("this is class A's __init__")
def method1(self, value):
print(value)
class B(A):
def __init__(self):
print("this is class B's __init__")
test = B()
test.method1('42')
When it says that the subclass overrides methods of the same name, it
means that if it finds the method in the subclass, it will stop looking
and use the one it found.
So in the example above when Python creates test it will find __init__
in B and so won't bother looking in A for it. However, when looking for
'method1' Python does not find it in B, and so looks in A for it and,
finding it there, uses that as B's method1.
If you want B's __init__ to also call A's __init__, you have to so
explicity:
def __init__(self):
A.__init__(self)
or
def __init__(self):
super(B, self).__init__()
or with Python 3
def __init__(self):
super().__init__()
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-08 07:25 -0800
Re: newb __init__ inheritance Maarten <maarten.sneep@knmi.nl> - 2012-03-08 08:30 -0800
Re: newb __init__ inheritance Peter Otten <__peter__@web.de> - 2012-03-08 18:03 +0100
Re: newb __init__ inheritance Ethan Furman <ethan@stoneleaf.us> - 2012-03-08 09:34 -0800
Re: newb __init__ inheritance "Colin J. Williams" <cjw@ncf.ca> - 2012-03-10 12:58 -0500
Re: newb __init__ inheritance "Colin J. Williams" <cjw@ncf.ca> - 2012-03-10 17:47 -0500
Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:18 -0700
Re: newb __init__ inheritance Chris Rebert <clp2@rebertia.com> - 2012-03-11 03:38 -0700
Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:56 -0700
Re: newb __init__ inheritance Chris Rebert <clp2@rebertia.com> - 2012-03-11 04:37 -0700
Re: newb __init__ inheritance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-11 05:40 -0600
Re: newb __init__ inheritance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-11 05:52 -0600
Re: newb __init__ inheritance Peter Otten <__peter__@web.de> - 2012-03-11 13:12 +0100
Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:56 -0700
Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-12 02:09 -0700
csiph-web