Ideas for Python 3

David MacQuigg dmq at gain.com
Wed Apr 28 14:35:32 EDT 2004


On Wed, 28 Apr 2004 18:33:24 +1200, Greg Ewing
<greg at cosc.canterbury.ac.nz> wrote:

>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.)

This seems much too brief for my taste.  I think some of the
"unnecessary words" will be very helpful for students.  I need other
opinions on this.

The distinction between calling from classes and calling from
instances is not necessary with the new syntax, because we don't have
the problem of needing a different calling sequence.  i.e. the student
doesn't have to remember cat1.talk() in one case and Mammal.talk(cat1)
in another.

With the new syntax, the distinction becomes important when we get to
bound and unbound functions (same as in Python), but that is later in
the chapter, not part of the introductory explanation.

I've started a new thread "Explanation of Instance Variables in
Python" so we can explore this topic more fully.

Thanks for your help.

-- Dave




More information about the Python-list mailing list