I. Binding methods and non-binding methods
Binding method: Bind to who should be called by WHO, who will call will be as the first parameter passed in
1. Methods bound to an object: a function defined in a class is bound to an object by default
2. Methods to bind to a class: add an adorner to the function defined in the class @classmethod
Two. Unbound method: Neither binding to the class nor binding to the object means that both the object and the class can be called, no matter who calls is a normal function, the normal function does not automatically pass the value of the effect
The cases are as follows:
class Foo: def F1 (self): Print (self) @classmethod def F2 (CLS): print(CLS) @ Staticmethod def F3 (A, B): print('f3 ', A * b)
ImportSettingsclassMysql:def __init__(self,ip,port): Self.ip=IP self.port=Port Self.id=self.create_id ()defTell_info (self):Print('ip:%s port:%s id:%s'%(self.ip,self.port,self.id)) @classmethoddeffrom_conf (CLS):returnCLS (Settings. Ip,settings. PORT) @staticmethoddefcreate_id ():ImportUUIDreturnuuid.uuid4 () obj1=mysql.from_conf () obj2= Mysql ('10.1.183.16', 8080) Obj1.tell_info () Obj2.tell_info ()Small case two. Isinstance and Issubclass
Isinstance (obj,cls) checks if obj is an object of class CLS
class Foo: Pass = Foo ()# type judgment print(isinstance (Obj,foo))
Issubclass (sub,super) Check if Sub class is a subclass of super class
class Foo: Pass class Bar (Foo): Pass = Foo ()# determines if subclass print(Issubclass (Foo,bar))
Three. Reflection
Python-oriented reflection in objects: manipulating Object-related properties in the form of strings, everything in Python is an object, replacing dots with strings (.) Operations on attributes
Hasattr (O,name) determines whether a property is present
SetAttr (Obj,name,value) gives the object an attribute, the property name to name, and the value to
GetAttr (O,name,default) Gets the value of the object Name property and does not declare that the property does not exist when the default error
Delattr (o,name) Delete the object's Name property
classPerson :def __init__(self,name,age): Self.name=name Self.age= AgedefRun (self):Print('%s Run'%self.name) obj= Person ('Yven', 18) delattr ()Print(Hasattr (obj,'name'))#' name ' in obj.__dict__Print(GetAttr (obj,'name', None))#obj.__dict__[' name ']SetAttr (obj,'Sex','male')#obj.__dict__[' sex '] = ' male 'Print(obj.sex) delattr (obj,'Sex')Print(obj.__dict__) setattr (obj,'Address','Shanghaipudong')Print(obj.address)Print(Hasattr (obj,'Address')) delattr (obj,'Address')Print(Hasattr (obj,'Address'))
Python Object-oriented (iii)