New statement proposal for Python
Rainer Deyke
root at rainerdeyke.com
Fri Jun 15 12:57:44 EDT 2001
"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9gceld02n4c at enews1.newsguy.com...
> "Rainer Deyke" <root at rainerdeyke.com> wrote in message
> news:JjgW6.284889$oc7.18565826 at news2.rdc2.tx.home.com...
> ...
> > The following two statements are basically identical:
> >
> > def f(x): return x
> > f = lambda x: x
>
> They're close, with one obvious difference:
>
> >>> def f(x): return x
> ...
> >>> f.__name__
> 'f'
> >>> f = lambda x: x
> >>> f.__name__
> '<lambda>'
> >>>
I'm quite aware of that, hence my use of the name 'basically'. In most
cases, '__name__' isn't important. In the case of functions, it is almost
exclusively used for tracebacks. If the traceback instead printed the line
of code from which the function was called, the user would almost always
have the same information, so '__name__' becomes redundant.
> You consider it a wart that more than one statement can bind
> variables? It's not just assignment and def -- class, import
> and from behave the same way. Notice one pattern: all kinds
> of non-assignment binding-statements deal with objects whose
> *NAMES* are significant *BOTH* in deciding the variable-name
> to bind *AND* to locating or initializing the object that is
> being bound. The f.__name__ issue above is not a "it just
> so happens". It's similar to, say:
> import mynicemodule
> vs
> mynicemodule = __import__('mynicemodule')
This is similar to what you have to do if you want a named object that is
not a class, function, or module:
class NamedObject:
def __init__(self, name): self.name = name
spam = NamedObject('spam')
If classes, functions, and modules require syntax sugar to prevent typing
the same thing twice, I see no reason why this syntax sugar cannot be
generic enough that it can do the same thing for 'NamedObject' above.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list