Re: [pygame] Name self not defined

2009-02-04 Thread Peter Gebauer
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  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
> [...@zhar.net - 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




Re: [pygame] Name self not defined

2009-01-29 Thread John Eikenberry
Ian Mallett wrote:

> Frankly, I like this solution:
> self.rect = self.rect.move(speedv or (0,0))
> Looks Pythonic to me.

+1

-- 

John Eikenberry
[...@zhar.net - 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


signature.asc
Description: Digital signature


Re: [pygame] Name self not defined

2009-01-29 Thread John Eikenberry
Jake b wrote:

> On Tue, Jan 27, 2009 at 9:18 PM, Yanom Mobis  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
[...@zhar.net - 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


signature.asc
Description: Digital signature


Re: [pygame] Name self not defined

2009-01-29 Thread Emile Kroeger
"or else", that's a good way of reading it, thanks.

On Thu, Jan 29, 2009 at 1:27 AM, Bill Coderre  wrote:
> Interestingly, one rarely sees people use "and" as an idiom. It will execute
> starting with the left-most statement, and keep going until one of them
> fails, then stop, executing nothing further. So you could read it as "and
> then"
>

Well, you often see things like:

enemy = FindClosestEnemy()
if enemy and enemy.can_attack(self):
self.pair_off_with(enemy)
else:
self.flail_around_madly()

... though it isn't really the "and" idiom you're talking about. That
would be more like:

self.attacked_this_turn = enemy and self:attack(enemy)

... which is indeed rather rare (rightfully so)

Emile


Re: [pygame] Name self not defined

2009-01-28 Thread Jake b
On Tue, Jan 27, 2009 at 9:18 PM, Yanom Mobis  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 )
-- 
Jake


Re: [pygame] Name self not defined

2009-01-28 Thread Bill Coderre

On Jan 28, 2009, at 4:06 PM, James Paige wrote:

The "or" operator is what they call a "short circuiting" operator. If
the first part is True then the second part is never evaluated. If the
first part is False, then the second part is returned.



Ah, the "or" idiom. Read it as "or else." I learned of this as a perl- 
ism of particular delight:

open(FILE) or die "Can't open the file!";



perl also has inverted tests:

die "The file is not there!" unless open(FILE);



Not trying to start LANGUAGE WARS here, just pointing out that when  
the idioms line up with common natural-language idioms, it's  
peculiarly satisfying.


Interestingly, one rarely sees people use "and" as an idiom. It will  
execute starting with the left-most statement, and keep going until  
one of them fails, then stop, executing nothing further. So you could  
read it as "and then"




Re: [pygame] Name self not defined

2009-01-28 Thread Yanom Mobis
oh, thanks.

--- On Tue, 1/27/09, James Paige  wrote:

From: James Paige 
Subject: Re: [pygame] Name self not defined
To: pygame-users@seul.org
Date: Tuesday, January 27, 2009, 9:24 PM


-Inline Attachment Follows-

On Tue, Jan 27, 2009 at 07:18:20PM -0800, Yanom Mobis wrote:
> this is the code in question:
> 
> class Car(Basicsprite): #car class
>     def __init__(self, speedarg=(0,0)):
>         self.speed = speedarg
>         Basicsprite.__init__(self)
>     def move(self, speedv=self.speed):
>         self.rect = self.rect.move(speedv)
> 
> 
> however, when I try to import that class from python:
> 
> 
> 
> >>> from basicsprite import Car
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "basicsprite.py", line 14, in 
>     class Car(Basicsprite): #car class    
>   File "basicsprite.py", line 18, in Car
>     def move(self, speedv=self.speed):
> NameError: name 'self' is not defined
> 
> 
> 
> how can self not be defined??

You can't use "self" in the "def" line. Here is how I usually accomplish 
what you are trying to do:

class Car(Basicsprite): #car class
    def __init__(self, speedarg=(0,0)):
        self.speed = speedarg
        Basicsprite.__init__(self)
    def move(self, speedv=None):
        if speedv==None: speedv = self.speedv
        self.rect = self.rect.move(speedv)


But maybe somebody else can suggest a nicer way to do that.

---
James Paige



  

Re: [pygame] Name self not defined

2009-01-28 Thread James Paige
The "or" operator is what they call a "short circuiting" operator. If 
the first part is True then the second part is never evaluated. If the 
first part is False, then the second part is returned.

---
James

On Wed, Jan 28, 2009 at 04:03:27PM -0800, Yanom Mobis wrote:
>how do you use the or statement in arguments?  
>i have never seen it done. 
>   
>--- On Wed, 1/28/09, Ian Mallett  wrote: 
>   
>  From: Ian Mallett          
>  Subject: Re: [pygame] Name self not defined  
>  To: pygame-users@seul.org
>  Date: Wednesday, January 28, 2009, 4:25 PM   
>   
>  Frankly, I like this solution:   
>  self.rect = self.rect.move(speedv or (0,0))  
>  Looks Pythonic to me.


Re: [pygame] Name self not defined

2009-01-28 Thread Yanom Mobis
how do you use the or statement in arguments?
i have never seen it done.

--- On Wed, 1/28/09, Ian Mallett  wrote:

From: Ian Mallett 
Subject: Re: [pygame] Name self not defined
To: pygame-users@seul.org
Date: Wednesday, January 28, 2009, 4:25 PM

Frankly, I like this solution:
self.rect = self.rect.move(speedv or (0,0))
Looks Pythonic to me.



  

Re: [pygame] Name self not defined

2009-01-28 Thread Ian Mallett
Frankly, I like this solution:
self.rect = self.rect.move(speedv or (0,0))
Looks Pythonic to me.


Re: [pygame] Name self not defined

2009-01-28 Thread Nicholas Dudfield

Damn, still haven't learned to format after all these years:

class Car(Basicsprite): #car class   
   def move(self, speedv=None):

   self.rect = self.rect.move(speedv or (0,0))


Re: [pygame] Name self not defined

2009-01-28 Thread Nicholas Dudfield

Marius Gedminas wrote:

On Tue, Jan 27, 2009 at 07:24:13PM -0800, James Paige wrote:
  
You can't use "self" in the "def" line. Here is how I usually accomplish 
what you are trying to do:


class Car(Basicsprite): #car class
def __init__(self, speedarg=(0,0)):
self.speed = speedarg
Basicsprite.__init__(self)
def move(self, speedv=None):
if speedv==None: speedv = self.speedv
self.rect = self.rect.move(speedv)


But maybe somebody else can suggest a nicer way to do that.



The idiomatic way to express that is 'if speedv is None:'.
See http://www.python.org/dev/peps/pep-0008/

Marius Gedminas

Don't know how `idiomatic` or `pythonic` it is but I see a lot of people use 
`or` expressions:

class Car(Basicsprite): #car class
   def move(self, speedv=None):
   self.rect = self.rect.move(speedv or (0,0))


no `speedv`, in (x,y) tuple format, is ever going to bool as False
so if it `is None` it will use `self.speedv` of (0,0)







Re: [pygame] Name self not defined

2009-01-28 Thread Sean Berry
Hey thanks for that link Marius :)

For anyone who's interested, here's the suggestion he was referencing:

"- Comparisons to singletons like None should always be done with 'is' or
'is not', never the equality operators."

On Wed, Jan 28, 2009 at 6:15 AM, Marius Gedminas  wrote:

> On Tue, Jan 27, 2009 at 07:24:13PM -0800, James Paige wrote:
> > You can't use "self" in the "def" line. Here is how I usually accomplish
> > what you are trying to do:
> >
> > class Car(Basicsprite): #car class
> > def __init__(self, speedarg=(0,0)):
> > self.speed = speedarg
> > Basicsprite.__init__(self)
> > def move(self, speedv=None):
> > if speedv==None: speedv = self.speedv
> > self.rect = self.rect.move(speedv)
> >
> >
> > But maybe somebody else can suggest a nicer way to do that.
>
> The idiomatic way to express that is 'if speedv is None:'.
> See http://www.python.org/dev/peps/pep-0008/
>
> Marius Gedminas
> --
> lg_PC.gigacharset (lg = little green men language, PC = proxima centauri)
>-- Markus Kuhn provides an example of a locale
>
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iD8DBQFJgGidkVdEXeem148RAkO0AJ9HCXWcvCbJeJxctMEoqVlTLXNRvQCgk1Oo
> vW/kT4jEnhftgSWgEDPo5o0=
> =Ijvy
> -END PGP SIGNATURE-
>
>


Re: [pygame] Name self not defined

2009-01-28 Thread Marius Gedminas
On Tue, Jan 27, 2009 at 07:24:13PM -0800, James Paige wrote:
> You can't use "self" in the "def" line. Here is how I usually accomplish 
> what you are trying to do:
> 
> class Car(Basicsprite): #car class
> def __init__(self, speedarg=(0,0)):
> self.speed = speedarg
> Basicsprite.__init__(self)
> def move(self, speedv=None):
> if speedv==None: speedv = self.speedv
> self.rect = self.rect.move(speedv)
> 
> 
> But maybe somebody else can suggest a nicer way to do that.

The idiomatic way to express that is 'if speedv is None:'.
See http://www.python.org/dev/peps/pep-0008/

Marius Gedminas
-- 
lg_PC.gigacharset (lg = little green men language, PC = proxima centauri)
-- Markus Kuhn provides an example of a locale


signature.asc
Description: Digital signature


Re: [pygame] Name self not defined

2009-01-27 Thread Jervis Whitley
On Wed, Jan 28, 2009 at 2:18 PM, Yanom Mobis  wrote:

> this is the code in question:
>
> class Car(Basicsprite): #car class
>def __init__(self, speedarg=(0,0)):
>self.speed = speedarg
>Basicsprite.__init__(self)
>def move(self, speedv=self.speed):
>self.rect = self.rect.move(speedv)
>
>
> however, when I try to import that class from python:
>
>
>
> >>> from basicsprite import Car
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "basicsprite.py", line 14, in 
>class Car(Basicsprite): #car class
>  File "basicsprite.py", line 18, in Car
>def move(self, speedv=self.speed):
> NameError: name 'self' is not defined
>
>
>
> how can self not be defined??
>
>
>
> This line contains your error.


> def move(self, speedv=self.speed):


At the time the default arguments are evaluated (at the time of function
definition), self - your instance - is not defined.

Try something along these lines instead:

def move(self, speedv=None):
  if speedv is None:
speedv = self.speed


Re: [pygame] Name self not defined

2009-01-27 Thread James Paige
On Tue, Jan 27, 2009 at 07:18:20PM -0800, Yanom Mobis wrote:
> this is the code in question:
> 
> class Car(Basicsprite): #car class
> def __init__(self, speedarg=(0,0)):
> self.speed = speedarg
> Basicsprite.__init__(self)
> def move(self, speedv=self.speed):
> self.rect = self.rect.move(speedv)
> 
> 
> however, when I try to import that class from python:
> 
> 
> 
> >>> from basicsprite import Car
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "basicsprite.py", line 14, in 
> class Car(Basicsprite): #car class
>   File "basicsprite.py", line 18, in Car
> def move(self, speedv=self.speed):
> NameError: name 'self' is not defined
> 
> 
> 
> how can self not be defined??

You can't use "self" in the "def" line. Here is how I usually accomplish 
what you are trying to do:

class Car(Basicsprite): #car class
def __init__(self, speedarg=(0,0)):
self.speed = speedarg
Basicsprite.__init__(self)
def move(self, speedv=None):
if speedv==None: speedv = self.speedv
self.rect = self.rect.move(speedv)


But maybe somebody else can suggest a nicer way to do that.

---
James Paige


[pygame] Name self not defined

2009-01-27 Thread Yanom Mobis
this is the code in question:

class Car(Basicsprite): #car class
def __init__(self, speedarg=(0,0)):
self.speed = speedarg
Basicsprite.__init__(self)
def move(self, speedv=self.speed):
self.rect = self.rect.move(speedv)


however, when I try to import that class from python:



>>> from basicsprite import Car
Traceback (most recent call last):
  File "", line 1, in 
  File "basicsprite.py", line 14, in 
class Car(Basicsprite): #car class
  File "basicsprite.py", line 18, in Car
def move(self, speedv=self.speed):
NameError: name 'self' is not defined



how can self not be defined??