[Python-ideas] A user story concerning things knowing their own names
Terry Reedy
tjreedy at udel.edu
Wed Mar 16 21:49:29 CET 2011
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.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list