A isinstance (OBJ,CLS) and Issubclass (Sub,super)
Isinstance (obj,cls) checks if obj is an object of class CLS
Class Foo (object): Pass obj = Foo () print (Isinstance (obj, foo))
Issubclass (sub, super) check if the sub class is a derived class of super class
Class Foo (object): Pass class Bar (foo): Pass Print (Issubclass (Bar, Foo)) >>true
Second, reflection
Python-oriented reflection in objects: manipulating Object-related properties in the form of strings. All things in Python are objects (you can use reflection)
Four functions to implement reflection:
Hasattr (object,name: To determine if there is a method or property in an object that has a name string corresponding to it
GetAttr (object, Name, Default=none): Get Property
SetAttr (x, ' Y ', V): Set property, is equivalent to ' x.y = V '
Delattr (x, ' y '): delete attribute, is equivalent to ' del x.y '
Four ways to use the demo:
Class Blackmedium:feature= ' Ugly ' def __init__ (self,name,addr): Self.name=name self.addr=addr def s Ell_house (self): print ('%s black intermediary sells a house, the idiot buys it, but who can prove that he is not a fool '%self.name) def rent_house (self): print ('%s Black intermediary rent House , the idiot just rented it '%self.name ' b1=blackmedium (' million into place ', ' Huilongguan Tian Lu Yuan ')#检测是否含有某属性Printhasattr (B1, ' name ')) Print (Hasattr (B1, ' Sell_house '))#获取属性n =getattr (B1, ' name ') print (n) func=getattr (B1, ' Rent_house ') func () # GetAttr (B1, ' aaaaaaaa ') #报错 Print (GetAttr (B1, ' aaaaaaaa ', ' not Present '))#设置属性setattr (B1, ' SB ', True) SetAttr (B1, ' Show_name ', Lambda self:self.name+ ' SB ') print (b1.__dict__) print (B1.show_name (B1))#删除属性delattr (B1, ' addr ')Delattr (B1, ' Show_name ') delattr (B1, ' show_name111 ') #不存在, error print (B1.__DICT__) four Ways to use demo
Third, __str__
__STR__: Defines a built-in binding method inside a class that prints automatically incoming objects
Class Teacher:def __init__ (self,name,age): Self.name=name self.age=age self.courses=[] def tea CH (self): print ('%s teach '%self.name) def __str__ (self): return ' <name:%s age:%s> '% (Self.name,sel F.age) class Course:def __init__ (self,name,price,period): Self.name=name self.price=price Self.per Iod=period def __str__ (self): return ' name:%s price:%s period:%s '% (self.name,self.price,self.period) EGON=TEAC Her (' Egon ', 18)print (Egon) #egon. __str__ () >>:<name:egon age:18>Python=course (' Python ', 20000, ' 6mon ') openstack=course (' OpenStack ', ten, ' 3mon ') linux=course (' Linux ', 1, ' 1mon ') Egon.courses.append (Python) egon.courses.append (OpenStack) Egon.courses.append (Linux) # egon.courses.extend ([ Python,openstack,linux])Print (egon.courses) for obj in egon.courses:
Print (obj)
>>:[<__main__. Course object at 0x0000027f4ff70748> <__main__. Course object at 0x0000027f4ff70780> <__main__. Course object at 0x0000027f4ff70710>] "Name:python price:20000 period:6mon" Name:openstack price:10 Period:3mon " Name:linux price:1 Period:1mon "
Iv. __del__
destructor, which automatically triggers execution when the object is freed in memory.
Import Timeclass Foo: def __init__ (self,x): self.x=x print (' Connect MySQL ') #conn =abcdef (' 192.168.1.10 ') , 3306) def __del__ (self): "Do some cleanup operations related to this object" " # Conn.close () # file.close () print (' ====> ') F=foo del f #f. __del__ () time.sleep (3) Print (' main program ') >>:connect mysql====> Main program F=foo (3 ) Print (' main program ') >>:connect MySQL main program ====>
Five, __ITEM__ series
Class Foo: def __init__ (self,name,age,sex): self.name=name self.age=age self.sex=sex def __ Getitem__ (self, item): # Print (Self,item,type (item)) # return GetAttr (Self,item) return self.__dict__[ Item] def __setitem__ (self, Key, value): # setattr (self,key,value) Self.__dict__[key]=value def _ _delitem__ (self, Key): # delattr (self,key) Self.__dict__.pop (key) def __len__ (self): return 10f=foo (' Egon ', ' Male ') print (f.name) #f [' Name ']print (f.age) #f [' Age ']print (f.sex) #f [' Sex ']print (f[' name ']) f[' Name ']= ' egon_nb ' Print (f.__dict__) del f[' name ']print (f.__dict__)
Print (Len (f))
>>:egon18maleegon{' name ': ' Egon_nb ', ' age ':, ' sex ': ' Male '} {' age ': ' Sex ': ' Male '}10
Python Basics (18) _ Object-oriented Programming 2 (reflection, __str__, __del__, __item__ series)