[Python-ideas] Callable properties

Ian Bicking ianb at colorstudy.com
Mon Jun 7 17:46:26 CEST 2010


On Mon, Jun 7, 2010 at 5:51 AM, George Sakkis <george.sakkis at gmail.com>wrote:

> I'm wondering if there is any downside in making properties callable:
>
> class callableproperty(property):
>    def __call__(self, obj):
>        return self.fget(obj)
>
> class Foo(object):
>    @property
>    def bar(self):
>        return self
>
>    @callableproperty
>    def baz(self):
>        return self
>
>
> >>> foo = Foo()
> >>> foo.baz is Foo.baz(foo)
> True
> >>> foo.bar is Foo.bar(foo)
> ...
> TypeError: 'property' object is not callable
>
>
> As for the motivation, having callable properties would make it easier
> to stack them with other decorators that typically expect callables.
> Am I missing something ?
>

I find stacking descriptors to be easier than it might at first appear (and
making decorators descriptors is also advantageous).  Treating properties
like Yet Another Descriptor helps here.  As it is you could use
Foo.bar.__get__(foo), or generally Foo.bar.__get__ as a callable.

-- 
Ian Bicking  |  http://blog.ianbicking.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100607/f8bf7e8f/attachment.html>


More information about the Python-ideas mailing list