Some legacy code I’m working with that works [code replaced by problem-reproducing code]:
class foo:
pass
class bar(foo):
def __new__(cls):
global BIZ
if BIZ is not None:
pass
bar()
but when I change it to
class foo(object):
then python prints:
Traceback (most recent call last):
File "test.py", line 11, in <module>
bar()
File "test.py", line 8, in __new__
if BIZ is not None:
NameError: global name 'BIZ' is not defined
Why is this?
The
__new__special method only applies to new-style classes (those inheriting directly or indirectly fromobject). Without subclassingobjectyour code isn’t getting called.