Unification of Methods and Functions

David MacQuigg dmq at gain.com
Fri Apr 30 12:47:05 EDT 2004


I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions.  Perhaps it was buried among too many other less important
changes, so in this thread I would like to focus on that issue alone.

I have edited the Proposed Syntax example below to take out the
changes unecessary to this discussion.  I left in the change of
"instance variable" syntax ( self.sound --> .sound ) because that is
necessary for the unification of all method forms.  ( Compare the
forms of the 'show' and 'talk' methods below.)

I believe these changes in syntax will make teaching OOP in Python
much easier.  See "Prototypes.doc" at
http://ece.arizona.edu/~edatools/Python/ The first eight pages are a
basic, but complete presentation of the new OOP syntax.  I expect this
to expand to about 30 pages with more examples and exercises.  This
compares to about 60 pages in Learning Python 2nd ed.

If we measure complexity by the number of pages needed for a "textbook
explanation" of OOP, then I believe the new syntax has some real
benefits. At this point in the learning process, students already know
functions, modules, and global variables.  The concept of using a
global variable __self__ is no surprise at all.  The only thing new in
a class, compared to a module, is the instance variables, and they can
be explained in one paragraph.  All methods look just like normal
functions.  There is no need to explain "static methods" or any other
form of method.  In fact, I use the term "function" rather than
"method" to emphasize the similarity.

I'm especially interested in feedback from users who are now learning
or have recently learned Python.  I already know these changes seem
trivial to many experts.  I've also heard plenty from the people who
think Python is so complex that we need to start a whole new language
( www.prothon.org ).  I'm looking for a middle ground.  I believe it
is possible to adopt what is good about Prothon, and not lose ten
years of software and community development.

======= Syntax Examples =============

## Proposed Syntax:
class Cat(Feline):
    numCats = 0
    def __init__( n = "unknown", s = "Meow" ):
        Feline.__init__()
        Cat.numCats += 1
        .name  = n           # Set instance variables.
        .sound = s
    def show():              # Define a "static method".
        Feline.show()
        print "    Cats:", Cat.numCats
    def talk():
        print "My name is ...", .name
        print "I am a %s from %s" % (.genus, .home)
        Mammal.talk()      # Call an unbound function.
        print __self__     ### Diagnostic check.
      
cat1 = Cat()            # Create instance.
bf = cat1.talk          # Make a bound function.


## Equivalent Python:
class Cat(Feline):
    numCats = 0
    def __init__(self, n = "unknown", s = "Meow" ):
        Feline.__init__(self)
        Cat.numCats += 1
        self.name  = n
        self.sound = s
    def show():
        Feline.show()
        print "    Cats:", Cat.numCats
    show = staticmethod(show)
    def talk(self):
        print "My name is ...", self.name
        print "I am a %s from %s" % (self.genus, self.home)
        Mammal.talk(self)
        print self

cat1 = Cat()            # Create instance.
bf = cat1.talk          # Make a bound function.

========= End of Examples =======

Thanks for your help.

-- Dave




More information about the Python-list mailing list