[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Name self not defined



Hi!

__new__ returns a new instance of the class while __init__
is simply used to initialize instance variables.
Normally you don't bother with __new__ unless you have a particular
need (does not happen to often in my experience with Python), some
types written in C may require it for deviating signatures of the 
constructor.

Also, I try to make it a rule of thumb to place any superclass calls
at the top of the __init__ method, this is useful to get a quick
overview of what superclass init's are called, in particular if you
have more than one. It's not possible or desirable in 100% of the cases,
but I try to do it that way for better readability. Just my personal
preference.

/Peter

On 2009-01-29 (Thu) 09:00, John Eikenberry wrote:
> Jake b wrote:
> 
> > On Tue, Jan 27, 2009 at 9:18 PM, Yanom Mobis <yanom@xxxxxxxxxxxxxx> wrote:
> > 
> > > this is the code in question:
> > >
> > > class Car(Basicsprite): #car class
> > >    def __init__(self, speedarg=(0,0)):
> > >        self.speed = speedarg
> > >        Basicsprite.__init__(self)
> > >
> > 
> > Is this valid to call parent constructor after other statements? ( Think
> > this is invalid in other languages  -- just not sure about python )
> 
> Yes. You can call the parents __init__() anywhere in the overridden method.
> But... __init__() is not the constructor in the traditional sense (as I
> understand it), it is already passed the constructed instance. In python
> the constructor is the __new__() method which is passed the class and you
> must call the superclass' __new__() to get the instance to work with.
> 
> -- 
> 
> John Eikenberry
> [jae@xxxxxxxx - http://zhar.net]
> [PGP public key @ http://zhar.net/jae_at_zhar_net.gpg]
> ______________________________________________________________
> "Perfection is attained, not when no more can be added, but when no more can be
> removed." -- Antoine de Saint-Exupery