Mport Types
Type (x) is types. Inttype # determine if int type
Type (x) is types. StringType #是否string类型
.........
--------------------------------------------------------
Super disgusting pattern, don't remember types. StringType
Import types
Type (x) = = types (1) # Determine if int type
Type (x) = = Type (' a ') #是否string类型
------------------------------------------------------
Using inline functions:
-
isi Nstance ( |
object, ClassInfo ) |
-
Return true if the Object argument is an instance of the ClassInfo argument, or of a (direct or indirect) subclass thereof. Also return True if classinfo is a type object and Object is An object of that T Ype. if object is not a class instance or an object of the given type, and the function always returns FALSE. if ClassInfo is Neither a class object nor a type object, it may be a tuple of class or type objects, Or may recursively contain other such tuples (other sequence types is not accepted). if ClassInfo is not a class, type, or tuple of classes, types, and such tuples, a typeerror exception is Raised. changed in version 2.2:support for a Tuple of type information was added.
Python can get the type of an object, using the type function:
>>>lst = [1, 2, 3]
>>>type (LST)
<type ' list ' >
Furthermore, you can use the Isinstance function to determine whether an object is a known type.
Isinstance is described below:
Isinstance (object, class-or-type-or-tuple), BOOL
Return whether an object are an instance of a class or of a subclass thereof.
With a type as second argument, return whether this is the object ' s type.
the form using a tuple, isinstance (x, (A, B, ...)), is a shortcut for
Isinstance (x, A) or isinstance (x, B) or ... (etc.).
Its first argument is an object, and the second is a list of type names or type names. Its return value is a Boolean type. Returns true if the type of the object is the same as the type of parameter two. If parameter two is a tuple, returns true if the object type is the same as one of the type names in the tuple.
>>>isinstance (LST, list)
Trueisinstance (LST, (int, str, list))
True
>>>isinstance (LST, (int, str, list))
True
This article is from the "Mr_computer" blog, make sure to keep this source http://caochun.blog.51cto.com/4497308/1548725
How to determine the data type of a variable in Python