isinstance

When is obj.GetType().IsInstanceOfType(typeof(MyClass)) true?

只愿长相守 提交于 2019-12-01 08:29:15
I'm looking at this piece of code written by someone else, and I'm wondering when it would evaluate to true. Basically, it is saying someType is an instance of someOtherType. Does it even make sense? So far, I've tried: derivedClass.GetType().IsInstanceOfType(typeof(BaseClass)) baseClass.GetType().IsInstanceOfType(typeof(DerivedClass)) myClass.GetType().IsInstanceOfType(typeof(MyClass)) And all of them evaluate to false. Any help is appreciated. Each of those 3 lines will return true only if the object involved ( derivedClass , baseClass and myClass respectively) is an instance of object , or

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

送分小仙女□ 提交于 2019-12-01 01:50:37
I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs , should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my attempts have been unsuccessful. Here's an SSCCE that should make isinstance return False in all cases but

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

本小妞迷上赌 提交于 2019-11-30 20:26:12
问题 I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs, should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my

issubclass of abstract base class Sequence

对着背影说爱祢 提交于 2019-11-29 15:21:45
This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__ , __len__ , __contains__ , __iter__ , __reversed__ , index , and count . So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self): return self def index(self, item): raise IndexError def count(self, item): return 0 issubclass(S,

Why is not 'decimal.Decimal(1)' an instance of 'numbers.Real'?

二次信任 提交于 2019-11-28 13:23:38
I try to check if a variable is an instance of a number of any type ( int , float , Fraction , Decimal , etc.). I cam accross this question and its answer: How to properly use python's isinstance() to check if a variable is a number? However, I would like to exclude complex numbers such as 1j . The class numbers.Real looked perfect but it returns False for Decimal numbers... from numbers Real from decimal import Decimal print(isinstance(Decimal(1), Real)) # False In contradiction, it works fine with Fraction(1) for example. The documentation describes some operations which should work with the

python isinstance vs hasattr vs try/except: What is better?

梦想与她 提交于 2019-11-28 09:58:19
I am trying to figure out the tradeoffs between different approaches of determining whether or not with object obj you can perform action do_stuff() . As I understand, there are three ways of determining if this is possible: # Way 1 if isinstance(obj, Foo): obj.do_stuff() # Way 2 if hasattr(obj, 'do_stuff'): obj.do_stuff() # Way 3 try: obj.do_stuff() except: print 'Do something else' Which is the preferred method (and why)? I believe that the last method is generally preferred by Python coders because of a motto taught in the Python community: "Easier to ask for forgiveness than permission"

How to check if an object is an instance of a namedtuple?

柔情痞子 提交于 2019-11-28 07:13:25
How do I check if an object is an instance of a Named tuple ? Alex Martelli Calling the function collections.namedtuple gives you a new type that's a subclass of tuple (and no other classes) with a member named _fields that's a tuple whose items are all strings. So you could check for each and every one of these things: def isnamedtupleinstance(x): t = type(x) b = t.__bases__ if len(b) != 1 or b[0] != tuple: return False f = getattr(t, '_fields', None) if not isinstance(f, tuple): return False return all(type(n)==str for n in f) it IS possible to get a false positive from this, but only if

Why is not 'decimal.Decimal(1)' an instance of 'numbers.Real'?

不问归期 提交于 2019-11-27 19:29:00
问题 I try to check if a variable is an instance of a number of any type ( int , float , Fraction , Decimal , etc.). I cam accross this question and its answer: How to properly use python's isinstance() to check if a variable is a number? However, I would like to exclude complex numbers such as 1j . The class numbers.Real looked perfect but it returns False for Decimal numbers... from numbers Real from decimal import Decimal print(isinstance(Decimal(1), Real)) # False In contradiction, it works

Comparing boolean and int using isinstance

心已入冬 提交于 2019-11-27 05:24:07
Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code. print isinstance(True, (float, int)) True My guess would be that its Python's internal subclassing, as zero and one - whether float or int - both evaluate when used as boolean, but don't know the exact reason. What would be the most pythonic way to solve such a situation? I could use type() but in most cases this is considered less pythonic. For historic reasons, bool is a subclass of int , so True is an instance of int . (Originally, Python had no bool type, and

How to check if an object is an instance of a namedtuple?

試著忘記壹切 提交于 2019-11-27 01:34:36
问题 How do I check if an object is an instance of a Named tuple? 回答1: Calling the function collections.namedtuple gives you a new type that's a subclass of tuple (and no other classes) with a member named _fields that's a tuple whose items are all strings. So you could check for each and every one of these things: def isnamedtupleinstance(x): t = type(x) b = t.__bases__ if len(b) != 1 or b[0] != tuple: return False f = getattr(t, '_fields', None) if not isinstance(f, tuple): return False return