[Python-ideas] A user story concerning things knowing their own names
Masklinn
masklinn at masklinn.net
Wed Mar 16 22:37:45 CET 2011
On 2011年03月16日, at 21:49 , Terry Reedy wrote:
> On 3/16/2011 12:11 PM, Michael Foord wrote:
>>>> On 16 March 2011 11:10, Larry Hastings <larry at hastings.org
>> As I suggested in my email on the Assignment Decorators thread this
>> morning, you could achieve this in current Python, no extension needed:
>>>> def assign(fn):
>> return fn(fn.__name__)
>>>> @assign
>> def content_size(name):
>> return overridable_property(name, "Size of the content area.")
>>> And building on this sightly you could do the following for namedtuple:
>> >>> from collections import namedtuple
>> >>> import inspect
>> >>> def make_namedtuple(fn):
>> ... args = ' '.join(inspect.getargspec(fn).args)
>> ... return namedtuple(fn.__name__, args)
>> ...
>> >>> @make_namedtuple
>> ... def Point(x, y): pass
>> ...
>> >>> p = Point(1, 2)
>> >>> p
>> Point(x=1, y=2)
>> If make_namedtuple were added to collections, then one could import *that* instead of 'namedtuple' itself.
Since `namedtuple` is a function, wouldn't it be possible to add this feature to the existing namedtuple, so that the number of names doing just about the same thing in slightly different ways doesn't explode? (it's just a different way of providing the exact same arguments after all)
More information about the Python-ideas
mailing list