Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21490
| References | (1 earlier) <jjg4p3$lst$1@theodyn.ncf.ca> <jjglmi$dao$1@theodyn.ncf.ca> <2feb13ca-f83b-4843-995f-ee77e7505ecd@db5g2000vbb.googlegroups.com> <mailman.560.1331462311.3037.python-list@python.org> <18769501.3541.1331463388634.JavaMail.geo-discussion-forums@ynkz21> |
|---|---|
| Date | 2012-03-11 04:37 -0700 |
| Subject | Re: newb __init__ inheritance |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.562.1331465879.3037.python-list@python.org> (permalink) |
On Sun, Mar 11, 2012 at 3:56 AM, hyperboogie <hyperboogie@gmail.com> wrote:
> 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:
>> <snip>
>> > thank you everyone...
>> > Still things are not working as expected... what am I doing wrong?
>> <snip>
>> > # 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`"?
Your classes should (ultimately) subclass the built-in class named
"object". In your case:
class A(object):
# …rest same as before…
This ensures that your classes are new-style rather than old-style
(the latter is deprecated); see:
http://docs.python.org/glossary.html#term-new-style-class
> 2. Is the mro function available only on python3?
There's never been an mro function. Perhaps you mean the __mro__
attribute of classes (e.g. `B.__mro__`), which is available in Python
2.2+ and Python 3?
Cheers,
Chris
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