The need to put "self" in every method

Piet van Oostrum piet at cs.uu.nl
Wed Jun 1 23:29:24 CEST 2005


>>>>> aahz at pythoncraft.com (Aahz) (A) wrote:

>A> [posted & e-mailed]
>A> In article <wzu0kj9jbi.fsf at ordesa.lan>,
>A> Piet van Oostrum  <piet at cs.uu.nl> wrote:
>>> 
>>> There is.
>>> Inside a method there are 3 kinds of identifiers:
>>> - local ones e.g. parameters and local variables
>>> - global ones (actually module-level)
>>> - instance variables and methods
>>> 
>>> Because Python has no declarations there must be a different way to
>>> indicate in which category an identifier falls. For globals it is done with
>>> the 'global' keyword (which actually is a declaration), for instance
>>> variables the dot notation (object.name) is used and the rest is local.
>>> Therefore every instance variable or instance method must be used with the
>>> dot notation, including the ones that belong to the object `itself'. Python
>>> has chosen that you can use any identifier to indicate the instance, and
>>> then obviously you must name it somewhere. It could have chosen to use a
>>> fixed name, like 'this' in Java or C++. It could even have chosen to use a
>>> keyword 'local' to indicate local ones and let instance ones be the
>>> default. But if instance variable would be implicit, local ones should have
>>> been explicit.

>A> Any objection to swiping this for the FAQ?  (Probably with some minor
>A> edits.)

No.
The global/local stuff needs a bit more nuance (assignments in the method
being the criterium).
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list