Tony wrote:

> On Aug 8, 9:28 pm, Peter Otten <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to