Need help with Python scoping rules

Dave Angel davea at ieee.org
Wed Aug 26 21:50:46 CEST 2009


Carl Banks wrote:
> On Aug 26, 8:13 am, Dave Angel <da... at ieee.org> wrote:
>   
>> You can probably work around this by replacing the staticmethod
>> decorator with an equivalent function call:.
>>
>> class Demo9(object):
>>     def fact(n):
>>         if n < 2:
>>             return 1
>>         else:
>>             return n * Demo.fact(n - 1)
>>
>>     _classvar =act(5)
>>     fact =taticmethod(fact)
>>
>> print Demo9._classvar
>> xx =emo9()
>> print xx.fact(6)
>> print Demo9.fact(8)
>>     
>
> This won't work normally.  It only worked for you because you made a
> typo.
>
>
> Carl Banks
>
>   
Sorry about the typo.  I was trying out several different versions of 
the class in the same module, and forgot to include to change Demo to 
Demo9 in the recursive call.

I didn't like that approach anyway, as it smacked of taking advantage of 
some implementation accident.  The other approaches are more 
straightforward.


DaveA



More information about the Python-list mailing list