[Python-ideas] A user story concerning things knowing their own names

Michael Foord fuzzyman at gmail.com
Wed Mar 16 17:11:29 CET 2011


On 16 March 2011 11:10, Larry Hastings <larry at hastings.org> wrote:

>
> On 03/16/2011 05:28 AM, Greg Ewing wrote:
>
> If I could write my overridable_property declarations
> without having to repeat the name, this kind of thing would
> not be able to happen.
>
>
> 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)


All the best,

Michael




> How bad do you want it?  ;-)
>
>
> /larry/
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>


-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110316/4e0578a9/attachment.html>


More information about the Python-ideas mailing list