2

私が使用しているいくつかのレガシーコードは機能します[コードは問題を再現するコードに置き換えられました]:

class foo:
    pass

class bar(foo):
    def __new__(cls):
        global BIZ
        if BIZ is not None:
            pass

bar()

しかし、私がそれを変更すると

class foo(object):

次に、Pythonは次のように出力します。

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

どうしてこれなの?

4

1 に答える 1

3

__new__特別なメソッドは、新しいスタイルのクラス(から直接または間接的に継承するもの)にのみ適用されobjectます。サブクラス化objectしないと、コードは呼び出されません。

于 2012-08-23T18:36:13.130 に答える