[Python-ideas] Callable properties

Michael Foord fuzzyman at voidspace.org.uk
Mon Jun 7 13:34:06 CEST 2010


On 7 June 2010 12:06, Masklinn <masklinn at masklinn.net> wrote:

> 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.
>

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:

>>> class Foo(object):
...  @property
...  def foo(self):
...   return 'foo'
...
>>> f = Foo()
>>> Foo.foo.__get__(f)
'foo'

The reason for wanting to do this are the same reasons as you would call an
unbound method.

Michael


>
> _______________________________________________
> 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/232f0bc6/attachment.html>


More information about the Python-ideas mailing list