Python 2:如何检查 created 类 的类型是否相等? (不是对象之间)

Python 2 : How do I check type equality with created classes ? (not between objects)

我有一个定义的class(里面的东西对问题没用):

class AllyInstance(dict):    
    def __init__(self,name,pyset,number,gender='none'):
        for dicopoke_loop_enemy in dicopoke[name]:
            self[dicopoke_loop_enemy]=dicopoke[name][dicopoke_loop_enemy]
        self['set']=pyset
        self['status']='Normal'
        self['hp']=pyset['stats']['hp']
        self['boosts']={'atk':0,'def':0,'spa':0,'spd':0,'spe':0}
        self['secondarystatuses']=[] #leech seed, confusion ...
        self['gender']=gender
        self['name']=name
        self['number']=number

我创建了一个 class 的实例:

someinstance=AllyInstance( - include here the stuff to enter which is rather long - )

稍后,我想测试某个实例的类型是否是 AllyInstance - 我的 class。但是

type(someinstance)==AllyInstance

产生错误。

这可能是因为询问 type(someinstance) 会产生:

__main__.AllyInstance

所以,我试试

type(someinstance)==__main__.AllyInstance

并返回 False。 (如果要求 __ main __ 没有意义,我不知道,我是初学者)

根据 How to check class equality in Python 2.5? 我可以简单地创建一个无用的 AllyInstance 实例并使用它来检查相等性。

但我仍然想知道如何在不创建无用实例的情况下继续操作,因为我真的很想避免这种情况。

一些东西,比如类型(一个对象)==(一个 class 名称)。

我应该如何进行?

您应该改用 if isinstance(someinstance, AllyInstance)

顺便说一句,把 "instance" 这个词放在 class 名字里不是个好主意。

if isinstance(foo, AllyInstance):

更多信息:python doc