Ideas for Python 3

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Apr 28 02:33:24 EDT 2004


David MacQuigg wrote:
> Explanation of Simplified Instance Variables
> --------------------------------------------
> """ Some of the variables inside the functions in a prototype have a
> leading dot.  This is to distinguish local variables in the function
> from "instance variables".  When a function is called from an instance
> ( cat1.talk() ) a special global variable __self__ is automatically
> assigned to that instance ( __self__ = cat1 )  Then when the function
> needs an instance variable ( .sound ) it uses __self__ just as if you
> had typed it in front of the dot ( __self__.sound )  The leading dot
> is just an abbreviation to avoid typing __self__ everywhere.  """

Explanation of Python Instance Variables, Omitting Unnecessary Words
--------------------------------------------------------------------

When a function is called from an instance (e.g. cat1.talk()),
the instance is passed in as an extra parameter at the beginning
of the parameter list, conventionally named 'self'. This allows
you to refer to attributes of the instance as 'self.attrname'
(e.g. self.sound).

========== END ==============

(I've left out mention of the difference between classes and
instances, as you did in your explanation even though it's just
as important in your scheme. Including it still wouldn't make
my explanation significantly longer than yours.)

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list