变量可以修饰吗?
Can variables be decorated?
python中的装饰函数或方法很精彩
@dec2
@dec1
def func(arg1, arg2, ...):
pass
#This is equivalent to:
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
我想知道是否可以在 python 中修饰变量。
@capitalize
@strip
foo = ' foo '
print foo # 'FOO'
#This is equivalent to:
foo = foo.strip().upper()
我通过搜索找不到任何关于该主题的内容。
否,装饰器语法仅对函数和 classes 有效。
只需将值传递给函数:
foo = capitalize(strip(' foo '))
或用结果替换变量:
foo = ' foo '
foo = capitalize(strip(foo))
装饰器的存在是因为您不能只在函数调用中包装函数或 class 声明;您 可以 .
的简单变量
python中的装饰函数或方法很精彩
@dec2
@dec1
def func(arg1, arg2, ...):
pass
#This is equivalent to:
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
我想知道是否可以在 python 中修饰变量。
@capitalize
@strip
foo = ' foo '
print foo # 'FOO'
#This is equivalent to:
foo = foo.strip().upper()
我通过搜索找不到任何关于该主题的内容。
否,装饰器语法仅对函数和 classes 有效。
只需将值传递给函数:
foo = capitalize(strip(' foo '))
或用结果替换变量:
foo = ' foo '
foo = capitalize(strip(foo))
装饰器的存在是因为您不能只在函数调用中包装函数或 class 声明;您 可以 .
的简单变量