class-call a function in a function -problem

Discussion in 'Python' started by wierus, Aug 16, 2005.

  1. wierus

    wierus Guest

    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....
     
    wierus, Aug 16, 2005
    #1
    1. Advertising

  2. wierus

    Larry Bates Guest

    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....
    >
    >
    >
     
    Larry Bates, Aug 16, 2005
    #2
    1. Advertising

  3. 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
     
    Steven Bethard, Aug 16, 2005
    #3
  4. Larry Bates wrote:
    > def __init__(self, x=1, y=2)

    [snip]
    > self.x=x
    > self.y=y
    > self.a=0
    > return
    >
    > def l(self):

    [snip]
    > self.a=self.x+self.y
    > print "In ludzik.l a=',self.a
    > return
    >
    > def ala(self):

    [snip]
    > self.l()
    > return


    Any reason for putting the return statements at the end of each function?

    STeVe
     
    Steven Bethard, Aug 16, 2005
    #4
  5. On Tue, 16 Aug 2005 18:46:30 +0200, "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

    ^
    '-+
    |
    +--(same name, so l=0 is replaced by l = the_subsequently_defined_function l)
    |
    +-,
    v
    > def l(self):
    > ludzik.l=ludzik.x+ludzik.y
    > print ludzik.l

    Within the above function, which will serve as a method of the class ludzik,
    you are using the name 'ludzik' as one would normally use 'self'. The consquence
    is that instances such as z below will all share ludzik as the place to access
    x, y, and l. This is legal, but not typically what you want.

    >
    > def ala(self):
    > print ludzik.x
    > print ludzik.y
    > ludzik.l()

    Think what ludzik.l is at this point. It is not zero, because the l=0 has
    been replaced with the def l(self): ...,
    but ludzik is the global name of your class, and an attribute lookup directly
    on a class gets you an unbound method (if the attribute is a function), and that
    is what you got.

    To get a bound method (meaning a method bound to the instance, so that the first
    argument (normally called 'self') is bound to the instance object), you have to
    call the method name as an attribute of the instance instead. I.e.,
    self.l()
    not
    ludzik.l()

    So if you change all the ludzik names inside l and ala methods, you should have better luck.

    >
    >
    >z=ludzik()

    The above created an instance z

    >z.ala()

    This created a _bound_ method z.ala, and called it with self bound to z,
    but you ignored self in ala, and instead of self.l -- which would have
    gotten you a bound method with l as the function and the same self (z)
    passed through -- you wrote ludzik.l, and got an unbound method, which
    complained because you didn't pass it the instance as the first arg.

    BTW, you could have done that explicitly by calling ludzik.l(self) at that
    point, for the same effect as the normal call of self.l()

    >====================================
    >
    >
    >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....
    >

    HTH
    Working through the tutorials is not a bad idea ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 16, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  2. wierus
    Replies:
    1
    Views:
    289
    =?iso-8859-1?q?Elmo_M=E4ntynen?=
    Aug 16, 2005
  3. grbgooglefan
    Replies:
    2
    Views:
    439
    Pascal Bourguignon
    Jan 30, 2008
  4. George2
    Replies:
    0
    Views:
    360
    George2
    Mar 17, 2008
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    203
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page