2
所以我試圖學會使用Python(2.x)裝飾器,並與他們混亂,我遇到......一個奇怪的事情。總結一下,我想我正在嘗試使用裝飾器將裝飾後的功能添加到其他地方的存儲器中。Python:裝飾器,範圍和模塊導入
我不知道這是否是最辛辣的做任何事情的方式,但我想明白出了什麼問題。
假設我有一個模塊(即我運行一個腳本)是這樣的:
# -*- coding: utf-8 -*-
# main.py
d = {}
b = []
def mydecorator(name):
b.append(name)
def decorator(fun):
d[name] = fun
print 'in here', d, b
return fun
print 'and here', d, b
return decorator
class SomeClass:
@mydecorator('a thing')
def working_func(self, params):
# do stuff
pass
def main():
# do stuff
print 'out there', d, b
if __name__ == '__main__':
main()
打印效果與預期:
and here {} ['a thing']
in here {'a thing': <function working_func at 0x7fd6b69e0758>} ['a thing']
out there {'a thing': <function working_func at 0x7fd6b69e0758>} ['a thing']
但是,如果我將全班學生分成單獨的模塊
# -*- coding: utf-8 -*-
# module.py
from main import mydecorator
class AnotherClass:
@mydecorator('boo')
def not_workin_func(self, params):
# do stuff
pass
並將其導入main.py
# -*- coding: utf-8 -*-
# main.py
import module
d = {}
b = []
def mydecorator(name):
b.append(name)
def decorator(fun):
d[name] = fun
print 'in here', d, b
return fun
print 'and here', d, b
return decorator
def main():
# do stuff
print 'out there', d, b
if __name__ == '__main__':
main()
變化列表和字典不堅持:
and here {} ['boo']
in here {'boo': <function not_workin_func at 0x7fd1009917d0>} ['boo']
out there {} []
我想,這已經是與Python和手柄的作用域/模塊導入?
你有一個循環導入,讓事情變得非常困難 - 嘗試將mydecorator從'main.py'移出到'module.py'或其他模塊中。 –
謝謝,將所有裝飾器的東西移動到另一個模塊中(從這個意義上說,如果我然後在'main.py'中導入'b','d',它們的內容與第一種情況相同)。所以主要問題是循環導入? – kekkonen