Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > class-call a function in a function -problem

Reply
Thread Tools

class-call a function in a function -problem

 
 
wierus
Guest
Posts: n/a
 
      08-16-2005
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....



 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      08-16-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....
>
>
>

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      08-16-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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      08-16-2005
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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      08-16-2005
On Tue, 16 Aug 2005 18:46:30 +0200, "wierus" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments