Home

Tony wrote:

> On Aug 8, 9:28 pm, Peter Otten <__pete...@web.de> wrote:
>
>> No, just wrong.
>>
>> >> class A:
>>
>> ... def alpha(self): return dir(self)[-2]
>> ... def gamma(self): return dir(self)[-1]
>> ...>>> a = A()
>> >>> a.alpha(), a.gamma()
>> ('alpha', 'gamma')
>> >>> a.beta = 42
>> >>> a.alpha(), a.gamma()
>>
>> ('beta', 'gamma')
>>
>> Peter

> Only wrong if the function is only to write its own name. if it does
> something else as well, seems to work:
>
> class a:
>
>         def square(self, x):
>                 print 'executing:', dir(self)[-1]
>                 print x*x
>         def cube(self, x):
>                 print 'executing:',     dir(self)[-2]
>                 print x*x*x
>
> b=a()
>
> b.cube(4),b.square(2)
> b.c =4
> b.cube(3), b.cube(2)

You mean

b.cube(3), b.square(2)

> executing: cube
> 64
> executing: square
> 4
> executing: cube
> 27
> executing: cube
> 8

Yeah, cargo cult programming, I love it.

dir() sorts attribute names alphabetically. Therefore the tail of the list
you are accessing will only be altered if you choose a name >
min(other_names), i. e. a name that comes after "cube" in the alphabet. Try
setting

b.root = 42

if you don't believe me.

Peter

previous
next

reliable unit test logging
Re: best GUI library for vector drawing program
Re: Why doesn't Python's "robotparser" like Wikipedia's "robots.txt" file?
Re: compile's problem?
Re: strings (dollar.cents) into floats
Fundacja Avalon
Mimo Wszystko
Fundacja Sloneczko
Mam Marzenie
Dzieci Niczyje