程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python analysis of isinstance function (all)

編輯:Python

Catalog

  • Preface
  • 1. Basic grammar
  • 2. Function application
  • 3. actual combat

Preface

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 :

  • type() , Do not consider inheritance relationships ( The subclass is not a parent type )
  • isinstance() , Consider inheritance relationships ( A subclass is a parent type )

1. Basic grammar

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

2. Function application

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))

3. actual combat

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. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved