BERNARD Julien
BERNARD Julien

Reputation: 229

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

I've got a defined class (what's inside is useless to the problem):

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

I create an instance of the class :

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

And later on, I want to test if someinstance's type is AllyInstance - my class. But

type(someinstance)==AllyInstance

yields False.

That is probably, because asking type(someinstance) yields:

__main__.AllyInstance

So, I try

type(someinstance)==__main__.AllyInstance

and it yields False. (if asking for __ main __ makes no sense, I don't know, i'm a beginner)

According to How to check class equality in Python 2.5? I could simply create an useless instance of AllyInstance and use this to check the equality.

But I'd still like to know how to proceed without creating useless instances, because I'd really like to avoid this.

Something, like type(an object)==(a class name).

How should I proceed ?

Upvotes: 0

Views: 109

Answers (2)

taesu
taesu

Reputation: 4570

if isinstance(foo, AllyInstance):

For more info : python doc

Upvotes: 3

BrenBarn
BrenBarn

Reputation: 251345

You should instead use if isinstance(someinstance, AllyInstance).

Incidentally, it is not a great idea to put the word "instance" in a class name.

Upvotes: 4

Related Questions