Connecting Tech Pros Worldwide Forums | Help | Site Map

class-call a function in a function -problem

wierus
Guest
 
Posts: n/a
#1: Aug 16 '05
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....



Elmo Mäntynen
Guest
 
Posts: n/a
#2: Aug 16 '05

re: class-call a function in a function -problem


On Tue, 16 Aug 2005 18:45:51 +0200, wierus wrote:
[color=blue]
> 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[/color]
#is this L or I^?[color=blue]
> def l(self):[/color]
#What about this^. You are strongly advised to use more obvious and more
easily distinguishable names.[color=blue]
> ludzik.l=ludzik.x+ludzik.y
> print ludzik.l[/color]
This is a normal instance method ^
[color=blue]
>
> def ala(self):
> print ludzik.x
> print ludzik.y
> ludzik.l()[/color]
If call normal method through a class you get an unbound method, so you
have to provide explicitly an instance of that class: ludzik.l(self). If
you don't want to do that, go read about class methods in the official
tutorial.
[color=blue]
>
>
> 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....[/color]

Elmo
Closed Thread