[Python-Dev] Re: new syntax for wrapping (PEP 318)

Jewett, Jim J jim.jewett at eds.com
Fri Feb 20 13:39:17 EST 2004


Summary:

With the current syntax, I think the cost is too high
for the benefit.  It might help to add a keyword, like

	def foo() wrapped_by [sugary, syntax, list]:

Details:

Phillip J. Eby:
>> As for the semantics of class decorators, I would propose that they be
>> identical to the semantics for function decorators.  I don't think
>> there's a point to adding more ways to spell '__metaclass__'.  :)

Paul Moore:
> I don't see anyone arguing for different *semantics*. 

In that very paragraph are two alternatives for what it 
would mean on a class.

One matches the function meaning for consistency. The other 
replaces __metaclass__, which someone thought would be more 
useful.

Personally, I see the PEP as useful, but not useful enough 
to justify having to remember new syntax.  Definately not 
useful enough to add new meaning to an unmarked list.
Alternative syntax won't make it more useful, but might
make it less costly to the rest of the language.

When I follow a def with an immediate assignment, it is 
usually to something other than the original name.  For 
instance, I may want to define a module-level function, 
even though I'm already within a function, or I might want 
to bind a second alias to the same function.  (Come to 
think of it, "alias" is also a different but logical meaning, 
particularly for the "as" format.)

The ugly solution is to use exec:

# Note that this string is just a string, and the editor
# won't help me.
fn_str = ("""
def %(name)s():
    print "in fn %(name)s"
globals()['%(name)s'] = %(name)s
""")

# Verified with 2.3, since multiple interpolation is subject
# to too many typos.
def makealpha():
    for fn in 'abcde':
        exec(fn_str % dict(name='%s' % fn))


I would love a better solution - but this proposal is too 
limited to help.  (= not enough benefit)  That's fine, so 
long as it is also easy to ignore. (= low cost) 

A new definition type similar to class or def would be OK; 
I just wouldn't use it much.  Changing the syntax of def is 
a bit more worrisome.

With this patch,

def foo():[sugary, syntax, list]
  and
def foo() [sugary, syntax, list]:

would have very different meanings.  

To be honest, when I first looked at Python, one of the selling
points was that it strongly encouraged readable code.  If I had
come across the above distinction too soon (like, say, when I 
wondered how to define a function or class), ... I'm not sure 
I would have continued to look.

def foo() wrapped_by [sugary, syntax, list]:

would be far less problematic, because the keyword 
(1)  serves notice that this is a special case
(2)  serves as a reminder of what the list does.
(3)  leaves open the possibility of future extensions with 
other keywords.

-jJ



More information about the Python-Dev mailing list