syntactic sugar idea for {static,class}methods
Some time ago, Gareth McCaughan suggested a syntax for staticmethods. You'd write class C(object): def static(arg) [staticmethod]: return 1 + arg C.static(2) => 3 The way this works is that the above becomes syntactic sugar for roughly: class C(object): def $temp(arg): return 1 + arg static = staticmethod($temp) Anyway, I thought this was a reasonably pythonic idea, so I implemented it, and thought I'd mention it here. Patch at: http://starship.python.net/crew/mwh/hacks/meth-syntax-sugar.diff Some other things that become possible:
class D(object): ... def x(self) [property]: ... return "42" ... hello! D().x '42'
(the hello! is a debugging printf I haven't taken out yet...)
def published(func): ... func.publish = 1 ... return func ... def m() [published]: ... print "hiya!" ... hello! m.publish 1
def hairy_constant() [apply]: ... return math.cos(1 + math.log(34)) ... hello! hairy_constant -0.18495734252481616
def memoize(func): ... cache = {} ... def f(*args): ... try: ... return cache[args] ... except: ... return cache.setdefault(args, func(*args)) ... return f ... def fib(a) [memoize]: ... if a < 2: return 1 ... return fib(a-1) + fib(a-2) ... hello! fib(40) 165580141 # fairly quickly
I'm not sure all of these are Good Things (esp. the [apply] one...). OTOH, I think the idea is worth discussion (or squashing by Guido :). Cheers, M. -- For every complex problem, there is a solution that is simple, neat, and wrong. -- H. L. Mencken
"MH" == Michael Hudson <mwh@python.net> writes:
MH> Some time ago, Gareth McCaughan suggested a syntax for MH> staticmethods. You'd write MH> class C(object): | def static(arg) [staticmethod]: | return 1 + arg | C.static(2) | => 3 Very interesting! Why the square brackets though? Is that just for visual offset or is there a grammar constraint that requires them? I'd leave them out of the picture, unless you mean to imply that a list is acceptable in that position <wink>. salt-and-pep-per-ly y'rs, -Barry
MH> class C(object): MH> def static(arg) [staticmethod]: MH> return 1 + arg BAW> Why the square brackets though? I believe Guido addressed this in his DevDay presentation. The list construct is to allow future extensions without requiring parser changes. Skip
"SM" == Skip Montanaro <skip@pobox.com> writes:
MH> s C(object): def static(arg) [staticmethod]: return 1 + arg BAW> Why the square brackets though? SM> I believe Guido addressed this in his DevDay presentation. SM> The list construct is to allow future extensions without SM> requiring parser changes. Okie dokie. -Barry
MH> class C(object): MH> def static(arg) [staticmethod]: MH> return 1 + arg
BAW> Why the square brackets though?
I believe Guido addressed this in his DevDay presentation. The list construct is to allow future extensions without requiring parser changes.
It was only one of the many grammar options I proposed, semi-jokingly. --Guido van Rossum (home page: http://www.python.org/~guido/)
Michael Hudson wrote:
Some time ago, Gareth McCaughan suggested a syntax for staticmethods. You'd write
class C(object): def static(arg) [staticmethod]: return 1 + arg
C.static(2) => 3
Nice! Note that this is quite similar to the [WebMethod] in C#, VB.Net, etc. , and indeed we could have [webmethod] for some variation of a SOAP/RPC interface. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm...
On Tuesday, February 12, 2002, at 06:05 PM, Michael Hudson wrote:
Some time ago, Gareth McCaughan suggested a syntax for staticmethods. You'd write
class C(object): def static(arg) [staticmethod]: return 1 + arg
C.static(2) => 3
At some point in the past, when the actual implementation wasn't even finished, I suggested to Guido to use class C(object): def static(class, arg): return 1 + arg as the syntactic sugar. I think he wasn't against it at the time, but somehow found the actual implementation more important:-) -- - Jack Jansen <Jack.Jansen@oratrix.com> http://www.cwi.nl/~jack - - If I can't dance I don't want to be part of your revolution -- Emma Goldman -
participants (6)
-
barry@zope.com -
David Ascher -
Guido van Rossum -
Jack Jansen -
Michael Hudson -
Skip Montanaro