[Python-ideas] Callable properties

George Sakkis george.sakkis at gmail.com
Mon Jun 7 21:05:00 CEST 2010

On Mon, Jun 7, 2010 at 5:46 PM, Ian Bicking <ianb at colorstudy.com> wrote:
> 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.

The problem though is that most existing decorators expect their input
to be a function or a callable at best, not a descriptor; they have to
do something like ``func = getattr(func, '__get__', func)`` to cover
all cases. But regardless, I'm not proposing to make all descriptors
callable (it probably doesn't make sense in general), just properties.


More information about the Python-ideas mailing list