Unification of Methods and Functions

From: David MacQuigg (dmq_at_gain.com)
Date: 04/30/04


Date: Fri, 30 Apr 2004 09:47:05 -0700

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



Relevant Pages

  • Re: Unification of Methods and Functions
    ... >would have the greatest concentration of unbound method use (my reasoning was ... staticmethod syntax is relatively new, and there is a long tradition ... in Python of using various alternatives to static methods. ...
    (comp.lang.python)
  • Re: Unification of Methods and Functions
    ... Moving them outside the ... inside and add the 'staticmethod' wrapper. ... and clients, who don't have time to get proficient in Python, and only ... That is one benefit of the proposed syntax. ...
    (comp.lang.python)
  • Re: - E04 - Leadership! Google, Guido van Rossum, PSF
    ... * BS in Computer Science or equivalent experience. ... you seem to know python. ... is a syntax trifle. ... sure to note the reciprocal "LIMITATION: ...
    (comp.lang.python)
  • Re: Unification of Methods and Functions
    ... > syntax for static methods is beneficial because it tells the ... but there's more - a static method is used in different ... Appendix 1 under a title of "Translating Python Classes to Prototypes" doesn't ... Class methods - this example just seems broken, ...
    (comp.lang.python)
  • Re: A __getattr__ for class methods?
    ... or am I trying to mirror a functionality that Python simply does ... I'm not sure that the way you tackled this is the good approach: while it's quite flexible as far as the search criteria go, it'll require less than obvious code to match keywords and values, will lead to somewhat verbose syntax, and the syntax itself is unforgiving, brittle and not really pythonic. ... As you probably know, Python has a good support for keyword args, allowing you to fill your arguments out of order. ... But **kwargs goes beyond the regular explicit keyword arguments: when specified, **kwargs is a dict populated with the implicit keyword arguments, the ones you haven't specified in the argument tuple of your method. ...
    (comp.lang.python)