Why no lexical scoping for a method within a class?
bdesth.quelquechose at free.quelquepart.fr
Wed Dec 17 22:11:19 CET 2008
walterbyrd a écrit :
> On Dec 17, 8:41 am, prueba... at latinmail.com wrote:
>> If scoping worked as you want, how, pray tell, would you define object
>> attributes?- Hide quoted text -
> I suppose you could do this:
> class className():
> varname = "whatever"
This defines a class attribute - that is, an attribute of the className
class object, accessible either thru the className object or it's
instances if not shadowed by an instance attribute by the same name/
> def fname(self, varname):
> . . . .
> Instead of having variable defined within methods to be global
> everywhere within the class.
There's nothing like a "variable defined within (a) method", because you
never define methods in Python - only functions. So there's no
difference in scoping rules for functions defined within a class
statement block or outside a class statement block.
> Anyway, it's not a matter of what I like, I am just trying to
> understand the reason behind the scoping rules.
Then you should start with understanding the scoping rules.
More information about the Python-list