Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I check if a given instance of a class belongs to a main class in python? [duplicate]

Tags:

python

suppose you have:

class F:
    pass

then you make an instance:

g=F()

how can i check if the instance g is derived from the main class F?

like image 774
ame_math Avatar asked Feb 08 '23 11:02

ame_math


1 Answers

You can do this:

if isinstance(obj, MyClass):
     print "obj is my object"

So for your example:

if isinstance(g, F):
    print "obj is my object"

Use this at your peril, sometimes it's Easier to Ask Forgiveness than Permission.

like image 123
K. Menyah Avatar answered Feb 09 '23 23:02

K. Menyah