Unification of Methods and Functions

Jack Diederich jack at performancedrivers.com
Fri Apr 30 13:59:44 EDT 2004


On Fri, Apr 30, 2004 at 09:47:05AM -0700, David MacQuigg wrote:
> 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.
> ======= 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 =======
> 

Explicit is better than implicit.
or
Magic BAAAAAAAAD [Phil Hartman as Frankenstein]

I suggest you check out perl to see mixing instance/class/static methods
in practice.  Becuase perl is weakly typed this 'makes sense' in perl, even if
it causes problems[1].  What happens in practice is bad, people write functions
that can be used in two or more ways.  This makes type checking hard, and
makes code unreadable.  Functions frequently do slightly different things when
called one way or another.

For a python version you could do type checking on the function by doing
static analysis of the code, but that would be unpythonic.  I like it when
a static function breaks badly when some yahoo tries to use self -- it breaks
early and loudly.  Your way might only break on a certain code path that
tries to access '.name' and turns the method from static to instance.
If you re-added staticmethod/classmethod to clear up the distinction then
the above example just becomes a new syntax for implicit 'self'.

A version of the ':vars: expression' lambda replacement gets suggested every
so often (and that exact syntax once by me).  It ain't going to happnen, labmda
is more likely to be dropped than enhanced.

-jackdied

[1] standard perl interview question, what is the difference between these
calls?  All of these end up calling meow() with one argument, but they all 
behave differently - sometimes very subtly.

$ob = new Cat;
$ob->meow(); # intsance method
Cat->meow(); # class method
Cat::meow($ob); # static method with first argument a Cat instance




More information about the Python-list mailing list