How class members are accessed
#!/usr/bin/env python# _*_coding:utf-8 _*_class pepole (object): ' This is __doc__ information!!! ' Country = "China" #静态字段 exists in class __city = "Beijing" #私有静态字段 def siyou (self): #私有字段在内部设置方法可以访问 return Pepo Le.__city def __init__ (self,name,age,weight): self.name = name Self.age = Age #普通字段, dynamic field, Object field exists in object Self.weight = Weight self.__name2 = "Fuck" #私有普通字段, the Def __call__ (self, *args, **kwarg) can also be accessed through internal indirect access or by special means s): print ' This is __CALL__!!! ' def func: #括号里至少一个参数 can be multiple print self.__name2 #内部简介访问私有字段 return "123" Def __infunc (self): #私有方法, Access print "This is __infunc!" through an internal introduction def run_infunc (self): #通过内部间接的访问私有方法 self.__infunc () @classmethod #类方法 def class_func (CLS): #括号里只能有一 A parameter CLS return "Class_method" @staticmethod #静态方法括号里不需要参数 (), can have a multi-parameter Def STS (): print "This is Stati Cmethod "@staticmethod #静态的私有方法, belongs to class, internal indirect access Def __static_fUNC (): #静态方法括号里不需要参数 (), can have a multi-parameter print "This was __static_func" Def run_static (self): #简介访问静态私有方法 pepole . __static_func () @property #属性把一个方法伪造成一个字段, a property, a property like a field rather than a function def att (self): #只能有一个self参数 return "p Roperty "Def __str__ (self): return" This is __str__ "#class Son (pepole): #继承people的派生类, cannot get a private method or field in the parent class # DEF S How to: #print pepole.__city# passobj = pepole ("Chenchao", 18,70) print obj.__doc__# View the annotation information in the class obj () #执行__c The All__ method appends () Printobj.country #执行静态字段print the object to the Obj.name #执行字段self. Name = "Chenchao" Print obj.siyou () #内部访问间接的调用私有 Field __city = "Beijing" Print Obj.func () #执行普通方法 def func (self):p rint pepole.class_func () #类方法 Call the class method @classmethodpe Pole.sts () #调用静态方法 @staticmethodprint obj.att #执行属性的方法 without parentheses @propertyobj. Run_infunc () #执行私有方法 (indirect access) def __f Unc_method () obj.run_static () #执行静态的私有方法 (indirect access) print pepole.__dict__ #获取类的成员, namely: Static field, method print obj.__dict__ #获取obj对象的成员 Normal field, private normal field print obj #类中定__str__ method that prints the return value of the default output method of the object print str (obj) #自动去类中找__str__方法并得到返回值from Test Import pepoleobj2 = Pepole ("Zhangsan", 22,66) Print obj2.__module__ #表示当前操作的对象在那个模块print obj2.__class__ #表示当前操作的对象的类是什么print obj._pepole__name2 #特殊方法访问对象中的私有普通字 Segment Print pepole._pepole__city #特殊方法访问类中的私有静态字段obj. _pepole__infunc () #特殊方法访问类中的私有方法 #obj2 = Son () #obj2. Show () #如果是继承的关系, Then the derived class cannot access private methods or private fields in the parent class
isinstance () issubclass ( )
#!/usr/bin/env python# _*_coding:utf-8 _*_a = 10class A (): print "This is Class A" class B (a): print "The is C Lass B "W = B () print isinstance (w,b) #True Determine if Object W is an object of class B print isinstance (w,a ) the base class of #True class is also possible print Isinstance (a,int) #Trueprint issubclass (b,a) #True to judge that B is a derived class or a is a base class for B print isinstance (b,int) # False
isinstance(obj= object , cls= class )
Check if obj is an object of class CLS is instance: instance
issubclass(sub= subclass , super= parent class )
to check if the sub class is a derived class of super class is
subclass:
Subclass
python-Object-oriented (four)--Summary of access methods for class members