[Python-Dev] classes and cell variables question

tomer filiba tomerfiliba at gmail.com
Tue Dec 19 23:34:58 CET 2006


> If you don't follow this reasoning, please write a counter-proposal
> so that people have something to shoot down.

?

i just wanted to be sure it was done on purpose, and what were the
reasons for that.



-tomer

On 12/20/06, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> tomer filiba schrieb:
> > my question is, how come classes don't create cell variables, like
> > normal functions?
>
> Not sure what you mean by "how come"? Why is the implementation
> reacting as it is? Because the body of class is compiled as
> a global code fragment, not as a nested one.
> Or why is the implementation the way it is? For several reasons,
> one being that classes predate nested functions.
>
> > was this done on purpose?
>
> Yes. Attributes defined inside a class are assumed to be accessed
> through attribute access *only*. So you write
>
>   self.foo()
>
> to invoke a method, instead of invoking
>
>   foo()
>
> Python treats methods and data attributes uniformly, so the
> same applies to data variables.
>
> > does it have to
> > do with inheritance? if so, what's wrong with my "bar" version?
>
> It also has to do with inheritance. If you do self.foo, it looks
> 1. in the object itself
> 2. in the class of the object
> 3. in the bases of the class of the object (recursively)
>
> It would be counter-intuitive if some things (i.e. things defined
> in the class itself) could be accessed directly, whereas other things
> (ie. attributes of the object itself, and things in the base classes)
> would need to be accessed through qualification. It would also be
> counter-intuitive if you find methods in an unqualified manner,
> but then can't call them because you didn't give a self argument.
>
> If you don't follow this reasoning, please write a counter-proposal
> so that people have something to shoot down.
>
> Regards,
> Martin
>


More information about the Python-Dev mailing list