Another Language Change for Debate
Gareth McCaughan
Gareth.McCaughan at pobox.com
Fri Jan 11 22:10:21 EST 2002
Matt Mueller wrote:
> After using some staticmethod() (in python2.2), it popped out at
> me how support for staticmethod/classmethod could be added without
> new keywords:
>
> class MyClass:
> def self.foo(x):
> #instance method
> def bar(y):
> #staticmethod
> def class.baz(z):
> #classmethod
>
> (Of course, since this would break ALL old code, adding new
> keywords would be probably be a better idea. ;)
I much prefer a different approach.
class MyClass:
def staticmethod bar(y):
blah()
stuff += stuff
def classmethod baz(z):
return 1
This generalizes in an obvious way:
def <name1> <name2>(<formals>): <body>
turns into
def <name2>(<formals>): <body>
<name2> = <name1>(<name2>)
which would make it possible, e.g., to define
a function called "memo" which memoizes its argument
and say (typical useless example follows):
def memo fib(n):
if n<2: return n
return fib(n-2)+fib(n-1)
and get only O(n) calls to "fib" on doing fib(n).
Perhaps there's an obvious reason why this doesn't
work, but I can't see what it might be right now.
Would it be difficult for the parser?
--
Gareth McCaughan Gareth.McCaughan at pobox.com
.sig under construc
More information about the Python-list
mailing list