class-call a function in a function -problem

Larry Bates lbates at syscononline.com
Tue Aug 16 12:56:41 EDT 2005


Try something like:


class ludzik:
    #
    # Define an __init__ method that gets called when
    # you instantiate the class.  Notice also that I've
    # allowed you to set x, and y parameters if you like.
    # If you don't pass them they default to 1 and 2 as
    # in your example.
    #
    def __init__(self, x=1, y=2)
        #
        # Create 3 attributes (x, y, a).  Note that I changed
        # your attribute named 'l' to 'a' because it conflicts
        # with your method named 'l'.
        #
        self.x=x
        self.y=y
        self.a=0
        return

    def l(self):
        #
        # Class attributes are referred to by self.<attributename>
        #
        self.a=self.x+self.y
        print "In ludzik.l a=',self.a
	return

    def ala(self):
        print "In ludzik.ala x=",self.x
        print "In ludzik.ala y=",self.y
        #
        # To refer to one of my own methods you use self.<methodname>
        #
        self.l()
        return


wierus wrote:
> Hello, i have a problem. I write my first class in python so i'm not a
> experience user.  I want to call a function in another function, i tried to
> do it in many ways, but i always failed:(
> I supposed it's sth very simple but i can't figure what it is:
> ==================================
> 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()
> 
> 
> z=ludzik()
> z.ala()
> ====================================
> 
> 
> k.py
> 1
> 2
> Traceback (most recent call last):
>   File "k.py", line 17, in ?
>     z.ala()
>   File "k.py", line 14, in ala
>     ludzik.l()
> TypeError: unbound method l() must be called with ludzik instance as
> first argument (got nothing instead)
> 
> 
> i would be gratefull for resolving this problem for me....
> 
> 
> 



More information about the Python-list mailing list