This function is a bit similar to type Definition of function
type Determine what the function type is , and isinstance By determining whether an object is of a known type
however isinstance Than type More advanced ( Functional differences )
Specific differences :
The source code of its basic functions is as follows :
def isinstance(x, A_tuple): # real signature unknown; restored from __doc__
""" Return whether an object is an instance of a class or of a subclass thereof. A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B) or ...`` etc. """
pass
isinstance(object, classinfo)
object : Instance object
classinfo : The type could be Direct or indirect class name 、 Basic types and tuples
Application of basic edition :
a = 10086
isinstance (a,int) # true
isinstance (a,str) # false
isinstance (a,(str,int,list)) # As long as one of the tuple types is satisfied , The answer is satisfaction , So for false
About python What are the specific data types of
Check out my previous articles :python Detailed analysis of data types ( The attached code )
Advanced version application :
s = () # Define a tuple type
isinstance(s,tuple) # true
isinstance(s,list) # false
s1 = [] # Define a list type
isinstance(s1,list) # true
s2 = {
} # Define a dictionary type
isinstance(s2,dict) # true
# Mixed type judgment , adopt for Index value judgment
isinstance(s,(tuple,list,dict))
combination python Of web Development
Define a field to get the field value
@python_2_unicode_compatible
class xx(Document):
name = StringField(required=False, help_text=u" name ")
age = StringField(required=False, help_text=u" Age ")
# Fields displayed in the log
_show_log = ('name', 'age')
def __str__(self):
return u" Code Nong studies the name of the monk :{name1}, Age :{name2}".format(name1=self.name,name2=self.age)
About format See my previous article for the function of :python in format Format function ( whole )
adopt @log Log comments to get
@log('addition')
def yy(name,age):
project = AudioBlack(name,age)
...
return project
stay log The log annotation is defined as follows :( Pseudo code )
def log(operation=''):
def _decorate(func):
@wraps(func)
def _wraps(*args, **kwargs):
x, y = kwargs.pop('log', True), kwargs.pop('operator', None)
z = func(*args, **kwargs)
# Log by default
if isinstance(z, (Document)) and x and y:
....
return z
return _wraps
return _decorate
The general logic is as follows :
Call specially at some place yy This function ,yy This function triggers the log through annotations , Then by calling xx This class ( These are the contents of development )
Return to today's focus :
log There is a in the log if isinstance(z, (Document))
function
Output is as follows :<class 'projects.models.project.xx'>
1. Scenario introduction Some
in the light of Python Beginne