【发布时间】:2017-10-30 04:42:51
【问题描述】:
使用python 2.7
我正在使用装饰器在导入时创建一个对象库,并在导入时对每个对象的实例进行一些检查;主要是重复检查...
我最近切换到使用 super() 以利用其多重继承处理,但它会在实例化的对象上引发 NameError。
简化代码高亮问题:
class Lib(object):
def __init__(self):
print "library created"
self.lib = {}
def add_obj(self, obj):
print "object being added to library --> %s" % obj.__name__
inst = obj()
print inst.name
self.lib[obj.__name__] = obj
def Reg(obj):
global test_lib
test_lib.add_obj(obj)
test_lib = Lib()
@Reg
class A(object):
def __init__(self):
object.__init__(self)
self.name = "A instance"
@Reg
class B(object):
def __init__(self):
super(B, self).__init__()
self.name = "B instance"
输出
>>> from testing import *
library created
object being added to library --> A
A instance
object being added to library --> B
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testing.py", line 25, in <module>
class B(object):
File "testing.py", line 14, in Reg
test_lib.add_obj(obj)
File "testing.py", line 8, in add_obj
inst = obj()
File "testing.py", line 27, in __init__
super(B, self).__init__()
NameError: global name 'B' is not defined
似乎存在范围问题?排除装饰器,B 类实例化没有问题。
有什么建议吗?
【问题讨论】:
标签: python python-2.7 super python-decorators