
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
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 ? George

On 2010-06-07, at 12:51 , George Sakkis wrote:
I'm wondering if there is any downside in making properties callable:
It already exists, it's called a method. Due to the way calling works in Python (you get a callable object and you apply the `()` operator to it) I don't think it's possible to discriminate based on the context to perform the same operation whether or not the value is called. Your best bet would probably be to wrap the output of the property in a subtype of itself (dynamically created subtype) able to return self on call. Or you just create a lambda wrapping the property call.

On 7 June 2010 12:06, Masklinn <masklinn@masklinn.net> wrote:
I think you misunderstood, he was suggesting making the property descriptor instances callable. Not a bad idea, but as a change to a builtin it would be covered by the language moratorium. Easy to do in a subclass of property though. At the moment you do the following, which is a bit ugly:
The reason for wanting to do this are the same reasons as you would call an unbound method. Michael

On 7 June 2010 12:34, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Or this which is still slightly ugly but at least gets rid of the magic method call:
Michael

On 7 June 2010 11:51, George Sakkis <george.sakkis@gmail.com> wrote:
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

On Mon, Jun 7, 2010 at 5:51 AM, George Sakkis <george.sakkis@gmail.com>wrote:
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

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

On 2010-06-07, at 12:51 , George Sakkis wrote:
I'm wondering if there is any downside in making properties callable:
It already exists, it's called a method. Due to the way calling works in Python (you get a callable object and you apply the `()` operator to it) I don't think it's possible to discriminate based on the context to perform the same operation whether or not the value is called. Your best bet would probably be to wrap the output of the property in a subtype of itself (dynamically created subtype) able to return self on call. Or you just create a lambda wrapping the property call.

On 7 June 2010 12:06, Masklinn <masklinn@masklinn.net> wrote:
I think you misunderstood, he was suggesting making the property descriptor instances callable. Not a bad idea, but as a change to a builtin it would be covered by the language moratorium. Easy to do in a subclass of property though. At the moment you do the following, which is a bit ugly:
The reason for wanting to do this are the same reasons as you would call an unbound method. Michael

On 7 June 2010 12:34, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
Or this which is still slightly ugly but at least gets rid of the magic method call:
Michael

On 7 June 2010 11:51, George Sakkis <george.sakkis@gmail.com> wrote:
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

On Mon, Jun 7, 2010 at 5:51 AM, George Sakkis <george.sakkis@gmail.com>wrote:
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

On Mon, Jun 7, 2010 at 5:46 PM, Ian Bicking <ianb@colorstudy.com> wrote:
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. George
participants (5)
-
George Sakkis
-
Guido van Rossum
-
Ian Bicking
-
Masklinn
-
Michael Foord