Python论坛  - 讨论区

标题:[python-chinese] python2.4 decorators

2006年07月20日 星期四 11:34

crocodile.liu crocodile.liu at 163.com
Thu Jul 20 11:34:00 HKT 2006

def require_int (func):
def wrapper (arg):
assert isinstance(arg, int)
return func(arg)

return wrapper

@require_int
def p1 (arg):
print arg

@require_int
def p2(arg):
print arg*2


在python文档上讲python2.4上讲decorators有这么一段代码
我运行p1(1)为什么会报错
>>> p1(1)
Traceback (most recent call last):
File "", line 1, in ?
TypeError: 'NoneType' object is not callable


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月20日 星期四 13:42

yi huang yi.codeplayer at gmail.com
Thu Jul 20 13:42:14 HKT 2006

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def require_int(func):
...   def wrapper(arg):
...     assert isinstance(arg,int)
...     return func(arg)
...   return wrapper
...
>>> @require_int
... def p1(arg):
...   print arg
...
>>> p1(1)
1
>>>


-- 
http://codeplayer.blogbus.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060720/261df7be/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月20日 星期四 13:43

limodou limodou at gmail.com
Thu Jul 20 13:43:22 HKT 2006

On 7/20/06, crocodile.liu <crocodile.liu at 163.com> wrote:
>
> def require_int (func):
> def wrapper (arg):
> assert isinstance(arg, int)
> return func(arg)
>
> return wrapper
>
> @require_int
> def p1 (arg):
> print arg
>
> @require_int
> def p2(arg):
> print arg*2
>
>
> 在python文档上讲python2.4上讲decorators有这么一段代码
> 我运行p1(1)为什么会报错
> >>> p1(1)
> Traceback (most recent call last):
> File "", line 1, in ?
> TypeError: 'NoneType' object is not callable
>

没有缩近啊。

-- 
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月20日 星期四 13:59

crocodile.liu crocodile.liu at 163.com
Thu Jul 20 13:59:20 HKT 2006

原来是直接粘上去的代码就不行了,手动在命令行中敲代码就好了


limodou 写道:
> On 7/20/06, crocodile.liu <crocodile.liu at 163.com> wrote:
>>
>> def require_int (func):
>> def wrapper (arg):
>> assert isinstance(arg, int)
>> return func(arg)
>>
>> return wrapper
>>
>> @require_int
>> def p1 (arg):
>> print arg
>>
>> @require_int
>> def p2(arg):
>> print arg*2
>>
>>
>> 在python文档上讲python2.4上讲decorators有这么一段代码
>> 我运行p1(1)为什么会报错
>> >>> p1(1)
>> Traceback (most recent call last):
>> File "", line 1, in ?
>> TypeError: 'NoneType' object is not callable
>>
>
> 没有缩近啊。
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号