2

SquishAutomationToolにPython言語を使用しています。このツールは、いくつかのカスタムオブジェクトと関数でPythonを拡張します。これは彼らがマニュアルで言っていることです:

SquishのPython固有の拡張モジュールは、次のステートメントに相当するものを内部的に実行することによって自動的にロードされます。

Python
import test
import testData
import object
import objectMap
import squishinfo
from squish import *

つまり、独自のスタンドアロンモジュールを開発しているのでない限り、それらを自分でインポートする必要はありません。

そうすることで、 (これに対して)自動的に再定義objectされるため、(のような)新しいスタイルのクラスを実行しようとするとエラーが発生します。class NewClass(object):

TypeError:メタクラスベースを呼び出すときにエラーが発生しました。module.__init__() 最大で2つの引数を取ります(3つ与えられます)

だから私はobject取り戻そうとしています。メタクラスに関するすばらしい記事を読んだ後object、次のコードで取得しようとしています。

class OrigObject:
    __metaclass__ = type

class NewClass(OrigObject):
    pass

object私の質問は、元のクラスから継承するのと同じですか?

更新:Python 2.4の使用に制限されています(それが重要な場合)

ありがとう!

4

2 に答える 2

7

From the very page you linked:

Squish's object module has the same name as the base class of all Python 2 new-style classes, and of all Python 3 classes. In practice this is very rarely a problem. For Python 2 we can just create old-style classes or do import __builtin__ and inherit from __builtin__.object instead of object. For Python 3 there is no need to do anything since we don't ever explicitly inherit object since it is inherited by default if no other class is specified.

So:

>>> import __builtin__
>>> __builtin__.object
<type 'object'>
>>> class SomeOldStyleClass():
...    pass
... 
>>> SomeOldStyleClass.__subclasses__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class SomeOldStyleClass has no attribute '__subclasses__'
>>> class SomeClass(__builtin__.object):
...    pass
... 
>>> SomeClass.__subclasses__()
[]

Although, I would note that I think this is an incredibly poor decision on the part of the creators of said module, they should have called it something else. Even if it's aimed at Python 3.x, if they are distributing it for 2.x, they should have thought for a moment, it would have done them no harm to call it something else, and by calling it object they create problems.

于 2012-04-23T14:06:59.473 に答える
2

This will get it for you: basestring.__bases__[0].

于 2012-04-23T14:02:11.260 に答える