A question on scope...
MRAB
python at mrabarnett.plus.com
Thu Jun 18 13:05:53 EDT 2009
Wells Oliver wrote:
> In writing out python classes, it seems the 'self' is optional, meaning
> that inside a class method, "self.foo = bar" has the same effect as "foo
> = bar". Is this right? If so, it seems a little odd- what's the rationale?
>
> Or am I mistaken?
>
Inside a function or method "foo = bar" would make "foo" local by
default. In an instance method an attribute "foo" of the instance needs
a reference to the instance itself, by convention "self", therefore
"self.foo". In a class method a reference to the class itself is by
convention "cls", therefore "cls.foo".
More information about the Python-list
mailing list