[Tutor] easy way to populate a dict with functions
Kent Johnson
kent37 at tds.net
Wed Aug 12 14:28:17 CEST 2009
On Tue, Aug 11, 2009 at 9:27 PM, bob gailer<bgailer at gmail.com> wrote:
> Kent Johnson wrote:
>>
>> On Tue, Aug 11, 2009 at 10:22 AM, bob gailer<bgailer at gmail.com> wrote:
>>
>>
>>>
>>> Decorator functions:
>>>
>>> def collect(func):
>>> 'a "decorator" that adds each function to the cmds list'
>>> cmds.append((func.__name__, func))
>>>
>>
>> Decorators must return a callable
>
> The docs indeed say that, but the interpreter does not enforce it. My
> example works as I designed it.
I guess if you just want to populate cmds it is OK but if you want the
name 'foo' to be bound to a function it fails:
In [20]: cmds = []
In [21]: def collect(func):
....: cmds.append((func.__name__, func))
In [22]: @collect
....: def foo(a):
....: return 'foo' * a
....:
In [23]: cmds
Out[23]: [('foo', <function foo at 0x14a1770>)]
In [24]: foo
In [25]: foo(3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/kent/<ipython console> in <module>()
TypeError: 'NoneType' object is not callable
Kent
More information about the Tutor
mailing list