tags (space delimited): Built-in methods
Built-in methods in object-oriented:
I, Isinstance (OBJ,CLS) and Issubclass (Sub,super)
- Isinstance (obj,cls) checks if obj is an object of class CLS;
For example:
class Foo(object): passobj=Foo()isinstance(obj,Foo)
- Issubclass (sub,super) Check if the sub class is a derived class of super class
For example:
class Foo(object): passclass Bar(Foo): passissubclass(Bar,Foo)
Say some of the main built-in methods:
Item Series: Viewing Properties
class Foo: def __init__(self,name): self.name=name def __getitem__(self,item):#获取对象的属性的内置方法,item='name' return self.__dict__.get(item) def __setitem__(self,key,value): print(key,value) def __delitem__(self,key): print(key) obj=Foo('egon')print(obj.__dict__)#以前获取属性通过如下的代码:obj.属性名字#现在获取属性,item系列就是把obj模拟成字典形式,来访问obj['name']#自动触发了__getitem__自动触发
Setting properties
Now what if you want to set properties? For example we want to set: obj[' sex ']= ' male ' as long as this setting, will trigger __setitem__
class Foo: def __init__(self,name): self.name=name def __getitem__(self,item):#获取对象的属性的内置方法,item='name' return self.__dict__.get(item) def __setitem__(self,key,value): self.__dict__[key]=value def __delitem__(self,key): print(key) obj=Foo('egon')#设置属性boj['sex']='male'print(obj.__dict__)print(obj.sex)
The result of the execution is:
[IMAGE.PNG-17.2KB] [1]
To delete a property:
- Del Obj.name
Del obj[' name '], this will trigger, the above __DELITEM__,SO code is:
class Foo: def __init__(self,name): self.name=name def __getitem__(self,item):#获取对象的属性的内置方法,item='name' return self.__dict__.get(item) def __setitem__(self,key,value): self.__dict__[key]=value def __delitem__(self,key): self.__dict__.pop(key)obj=Foo('egon')#删除属性del obj['name']print(obj.__dict__)
Execution Result:
[IMAGE.PNG-18.6KB] [2]
__STR__ Series:
Before we said {' Name ': ' Egon '} This dictionary is essentially the fact that we call Dict () and then pass in the parameters, for example:
Dict ({' Ggname ': ' Egon '})
As above we can use: d=dict ({' name ': ' Egon '})
Print (Isinstance (d,dict))
Execution Result: True
Description: D is an example of dict;
- For Python's own classes, they come with a print-only feature that prints out not an object's address:
[IMAGE.PNG-62.5KB] [3]
Execution Result:
[IMAGE.PNG-29.6KB] [4]
As mentioned above I added: Name and age, result printing results or Strength object address:
[IMAGE.PNG-91.1KB] [5]
Execution Result:
[IMAGE.PNG-19.8KB] [6]
As we can see in the code above, what are the attributes that are useful to us when instantiating an object: Name and age, then we need to customize a method to print out the information that is useful to me when he is on the print () object, what is the built-in method?
Is:
__str__ Method:
[IMAGE.PNG-78.9KB] [7]
In fact, when we execute: print (obj) is also equal to print (obj.str())
class People: def __init__(self,name,age): self.name =name self.age=age def __str__(self): return'<name:%s,age:%s>' %(self.name,self.age)obj =People('egon',18)print(obj)
If the above code, in the automatic transfer of print (obj) back, back to automatically adjust the __str__ this method;
del
Before we learned how to manipulate files,
f=open('a.txt')f.read()f.close()
involves two aspects of knowledge: the execution principle of the above code is;
A.txt is placed in the hard disk, the program is not directly operating the hard disk, the program is through the operating system, and then have the operating system to operate the hard disk files, so we open the file, to close, the purpose is to recycle the operating system resources, because all the resources can not keep you occupied;
- Recycle the resources of the operating system before you close the program;
class Open: def __init__(self,filename): self.filename=filename def __del__(self): print('del....回收操作系统资源,self.close()')f=Open('setttings.py')print('------main-------')#这是python 自动给你执行的del也可以自己执行:del f # f.__del__()
- Built-in ——-del--(), when the object is deleted, Python automatically calls __del__ () this method;
Object-oriented built-in approach