A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/2006-December/070355.html below:

[Python-Dev] classes and cell variables question

[Python-Dev] classes and cell variables question"Martin v. Löwis" martin at v.loewis.de
Tue Dec 19 23:29:30 CET 2006
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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4