class-call a function in a function -problem

Steven Bethard steven.bethard at gmail.com
Tue Aug 16 19:47:02 CEST 2005


wierus wrote:
> class ludzik:
>  x=1
>  y=2
>  l=0
>  def l(self):
>   ludzik.l=ludzik.x+ludzik.y
>   print ludzik.l
> 
>  def ala(self):
>   print ludzik.x
>   print ludzik.y
>   ludzik.l()

Methods defined in a class expect an instance of that class as the first 
argument.  When you write:
     ludzik.l()
you are not passing any instance to the l() method.  Instead you should 
write:
     self.l()
Note that "self.l()" gets translated into the equivalent of 
"ludzik.l(self)" internally in Python.  That is, the method ludzik.l 
gets called with "self" as the first parameter.

In general, I'd be surprised if you really want all those "ludzik.XXX" 
attribute accesses.  I'd expect that most of those should really be 
"self.XXX" accesses.  You want to modify the instance (called "self" in 
your methods), not the class (called "ludzik" in your example).

STeVe



More information about the Python-list mailing list