On Tue, 16 Aug 2005 18:46:30 +0200, "wierus" <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list