[Python-ideas] Callable properties

Michael Foord fuzzyman at voidspace.org.uk
Mon Jun 7 13:46:19 CEST 2010


On 7 June 2010 11:51, 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 ?
>

Not sure it would specifically help with stacking decorators on properties
though. If you get them in the wrong order then you would not end up with a
property descriptor in the class dict but with an arbitrary callable (or
function which would be wrapped as a method) that no longer behaves as a
property.

Michael


>
> George
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
http://www.voidspace.org.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100607/0e454778/attachment.html>


More information about the Python-ideas mailing list