class-call a function in a function -problem

W

wierus

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

Larry Bates

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
 
S

Steven Bethard

wierus said:
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
 
S

Steven Bethard

Larry said:
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
 
B

Bengt Richter

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top