我收到的错误令我非常难过。
#! /usr/bin/env python3
from sys import modules
from inspect import isclass
from functools import partial
from abc import abstractmethod
from abc import ABC as AbstractClass
class Decoration(AbstractClass):
defaults = {}
def __init__(self, *list, **map):
self.list = list
self.map = map
def __get__(self, caller, owner):
return partial(self.__call__, caller)
@abstractmethod
def __call__(self, decoratee, *list, **map):
pass
@staticmethod
def set_defaults(defaults):
Decoration.defaults = defaults
class Decorator(Decoration):
def __init__(self, *list, **map):
super().__init__(self, *list, **map)
def __call__(self, function, *list, **map):
print(function)
def decorated(*list, **map):
if map:
for decorator in map:
for arguments in map[decorator]:
function = getattr([__name__], decorator)(function, arguments)
return function(*list, **map)
return decorated
@Decorator()
def dummy(*list, **map):
for item in list:
print(item)
for k, v in map:
print('{} {}'.format(k, v))
def main():
dummy({'banana': True})
if __name__ == '__main__':
main()
在“function = getattr ...”行中,我收到此错误:
UnboundLocalError: local variable 'function' referenced before assignment
我很困惑主要是因为上面的print语句将函数的定义发现为
<function dummy at (address)>
这是正确的。此外,删除该行可使程序正常运行并返回当前下一行。据我所知,这不是getattr的语法问题。有什么想法吗?