【发布时间】:2012-10-16 09:20:06
【问题描述】:
这更像是一个语法错误问题,我正在尝试在 Python 装饰器上完成本教程
http://www.learnpython.org/page/Decorators
我尝试的代码
def Type_Check(correct_type):
def new_function(old_function):
def another_newfunction(arg):
if(isintance(arg, correct_type)):
return old_function(arg)
else:
print "Bad Type"
#put code here
@Type_Check(int)
def Times2(num):
return num*2
print Times2(2)
Times2('Not A Number')
@Type_Check(str)
def First_Letter(word):
return word[0]
print First_Letter('Hello World')
First_Letter(['Not', 'A', 'String'])
我想知道怎么回事,请帮忙
【问题讨论】:
-
另外,您的命名选择很差。函数名应该是小写的(见pep-8),像“another_newfunction”这样的名字真的没有意义。