Syntax modification idea: (classmethods, properties locking...)

Jeff Epler jepler at unpythonic.net
Thu Jan 16 23:37:07 CET 2003


On Thu, Jan 16, 2003 at 10:03:12PM +0100, Arne Koewing wrote:
> Hi!
> After reading some treads about 
> "A Hygienic Macro System in Python"
> http://mail.python.org/pipermail/python-dev/2002-March/021369.html
> http://mail.python.org/pipermail/python-dev/2002-March/021467.html
> i've had an idea:
> 
> why not extending the def statement?
> 
> class bar(object):
>     def foo(class,arg1) is classmethod:
>         pass
>     def bar(arg1) is staticmethod:
> 	pass
>     
> def X(...) is Y:   #don't know if 'is' is realy a goog idea...
>   <<block>>

Something like this was suggested once, possibly on python-dev.  The
syntax proposed there was:
    def f() [classmethod]:
        pass
A chained form was also proposed.

The proposal did not win any wide acceptance.

I also wonder if trick used to make 'import ... as' legal without making
'as' a keyword could work in this situation, as in:
    funcdef: 'def' NAME NAME? parameters ':' suite
If the second NAME is present, then it is the name of the function and
the first NAME is the function's transformer.  This could be applied to
classes as well:
    classdef: 'class' NAME NAME? ['(' testlist ')'] ':' suite
if both names are specified, then the first is the metaclass.  Examples
(assuming an appropriate definition of the memoized wrapper & singleton
metaclass):

    # Equivalent to
    # class S:
    #     __metaclass__ = singleton
    #     ....
    class singleton S: ...
    
    # Equivalent to
    # def f(x): ...
    # f = memoized(f)
    def memoized f(x): ...

lowercase 'singleton' looks better to me than the alternative, 'class
Singleton S', same for 'memoized' vs 'Memoized'.

However, in this proposal, the following can't work
    def locked_with("resource") f(x): ...
you're forced to do
    locked_with_resource = locked_with("resource")
    def locked_with_resource f(x): ...
instead.  Also, no chained form is possible.

Jeff





More information about the Python-list mailing list