comparing types - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: comparing types (/thread-27866.html) |
comparing types - Skaperen - Jun-25-2020 my function gets 2 arguments, foo and bar. they need to be the same type. verifying this is pretty obvious: if type(foo) is type(bar): ...or should i use == instead of is ? is this the right way to go (ignoring those who say i should only support one type)?
RE: comparing types - Gribouillis - Jun-25-2020 It is the right way to go. Observe that it will also return False if for example type(foo) is a subclass of type(bar)
RE: comparing types - Skaperen - Jun-26-2020 isinstance(foo,type(bar)) ?
RE: comparing types - Gribouillis - Jun-26-2020 Now you are changing the specifications. What if bar has a subtype of type(foo)? RE: comparing types - DeaD_EyE - Jun-26-2020 There is a issubclass built-in.class Bar: ... class Foo(Bar): ... foo_inst = Foo() bar_inst = Bar() type(bar_inst) in type(foo_inst).mro() # but easier is the subclasscheck issubclass(type(foo_inst), type(bar_inst)) RE: comparing types - Skaperen - Jun-26-2020 (Jun-26-2020, 07:08 AM)Gribouillis Wrote: Now you are changing the specifications. What if bar has a subtype of type(foo)? if either is a subtype of the other. i'll have to decide if that should be considered the same. i need another value for bool ... Maybe. if i make this into a function, it could return 3 different int values. |