How to determine if an argument is an object or inline?

How to determine that a "variable" is built as a string, list, dict or number, rather than an "object". I am trying to make a deepcopy-ish function for dicts that copies inline types but ignores objects.

+4
source share
8 answers

Everything is an object. If you want to find out if a particular object is a string or an integer or another type, use isinstance() :

 >>> isinstance("hello!", str) True >>> isinstance("hello!", int) False 

So, in your case, it seems that you want to call the function only with dictionaries:

 >>> mylist = [3, "cabbage", 5.43, {'cat':'dog'}, {'horse':'elephant'}, ['another', 'list']] >>> for i in mylist: ... if isinstance(i, dict): ... dostuff() 
+3
source

Since there are several built-in types, you can check usage if:

 if type(a)==int or type(a)==float or type(a)==str or type(a)==list or type(a)==dict: #do something 

or use isinstance()

 if isinstance(a,(int,float,str,list,dict,set)): #do something 

I don’t know whether to do it right. But this is one way to check if a given variable is an instance of any built-in data type.

+2
source

I think that using the class.__module__ , which contains the name of the module where the class was defined, is the best way:

 >>> import __builtin__ >>> int.__module__ == __builtin__.__name__ True 

Please note that although the built-in module is automatically imported, we must import it in order to bring it into scope and get its name.

For a user-defined class:

 >>> class A: pass ... >>> A.__module__ '__main__' 
+1
source

First of all, this is an object, so I assume that you want to test built-in types depending on user-defined types.

Select the built-in module that you want to exclude, and compare the type () variable.

 if type(yourvar) in [list,set,int,float,str,dict]: print "builtin" else: print "object" 

Isinstance usually prefers to compare type . However, isstance will be True for objects that extend the built-in types:

 >>> class A(list): pass >>> a = A() >>> isinstance(a,list) True 

Therefore, if you want strictly built-in types, you should not use isinstance.

+1
source

Only CPython (may work in other implementations):

 import __builtin__ builtin_types = [x for x in __builtin__.__dict__.values() if isinstance(x, type)] def is_builtin_type(x): for t in builtin_types: if isinstance(x, t): return True 

This covers the built-in exceptions, bytearrays, objects, and a couple of others that the guys didn't mention in other answers :-)

0
source

First of all, in Python there is no difference between classes and types:

 >>> type("234"), "234".__class__ (str, str) 

Then type testing for objects can have two different values:

isinstance checks if your object has a given type or subtype:

 >>> class mystr(str): pass >>> s = mystr(4) >>> s '4' >>> ype(s) __main__.mystr >>> isinstance(s, str) True 

then

 >>> type(s) is str False >>> type(s) <class '__main__.mystr'> 

by the way, you should not write type(s) == str , but type(s) is str .

Now you can answer your question: __builtin__ built-in type module

 >>> str.__module__ '__builtin__' >>> mystr.__module__ '__main__' 

So you can probably write

 >>> def is_of_builtin_type(obj): return type(obj).__module__ == '__builtin__' >>> is_of_builtin_type("4223") True >>> class mystr(str): pass >>> is_of_builtin_type(mystr(223)) False 

Note. I have not tested how safe it is.

0
source

FYI, I went with this solution: (inspired by this )

 from six import integer_types, string_types, text_type _copyable_types = ( integer_types, string_types, text_type, list, dict, set, ) def deepish_copy(org): ''' Will copy a dict but ignore user objects at top level. ''' out = {} for k, v in org.iteritems(): if isinstance(v, _copyable_types): try: out[k] = v.copy() # dicts, sets except AttributeError: try: out[k] = v[:] # lists, tuples, strings, unicode except TypeError: out[k] = v # ints return out 
0
source

Direct

For a given var variable, the isinstance(var, type) function returns True if var is of type type and False otherwise. You can easily check this in the interpreter:

 >>> a = 2 >>> isinstance(a, int) True >>> b = 'correct' >>> isinstance(b, int) False >>> c = [1,2,3,4] >>> isinstance(c, list) True 

... and so on. Therefore, if you want to check that item is a dict :

 if isinstance(item, dict): # handle the dict, do your deep copy else: # meh, whatever 

As an alternative

Consider using the types module, which allows you to:

 from types import DictType def dictate(item): if isinstance(item, dict): # handle the dict else: # panic! 
-2
source

Source: https://habr.com/ru/post/1485747/


All Articles