Code snippet: dualmethod descriptor

Arnaud Delobelle arnodel at googlemail.com
Sat Feb 6 17:08:55 EST 2010


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Thu, 04 Feb 2010 00:09:02 -0300, Gabriel Genellina wrote:
>
>> En Sat, 30 Jan 2010 03:06:18 -0300, Steven D'Aprano
>> <steve at remove-this-cybersource.com.au> escribió:
>> 
>>> class dualmethod(object):
> [...]
>
>> Seems useful!
>> Perhaps a better place to post it would be
>> <http://code.activestate.com/recipes/langs/python/>, at least it's
>> easier to search.
>
>
>
> Thank you for the encouragement. The recipe is now posted:
>
> http://code.activestate.com/recipes/577030/

Oddly, in Python 3 you can achieve something a bit similar very simply:

>>> class A:
...     def foo(self=None):
...         return "Instance" if self else "Class"
... 
>>> A.foo()
'Class'
>>> a = A()
>>> a.foo()
'Instance'

It works in python 3 because unbound methods are plain functions.

-- 
Arnaud



More information about the Python-list mailing list