Python full stack learning -- object-oriented advanced 3

review:

#Reflection must know, must be able to read and understand, must know where to use it

#hasattr  getattr setattr delattr

Built-in methods must be understandable and usable

The result of __len__len(obj) depends on the result of obj.__len()__ to calculate the length of the object

The result of __hash__ hash(obj) depends on the result of obj.__hash__() to settle the hash value of the object

The result of __eq__ obj1 == obj2 depends on the result of obj.__eq__() to determine equality

__str__ str(obj) print(obj) The result of '%s'%obj depends on __str__, which is used for output and display

__repr__ repr(obj) The result of '%r'%obj depends on __repr__, and can also be used as a spare tire for str

The result of __format__ format() depends on the result of __format__, which is object formatted

__call__ obj() is equivalent to calling __call__, the object that implements __call__ is callable

 The __new__ constructor, executed before __init__ is executed, is responsible for creating an object, which has specific applications in the singleton pattern

The __del__ destructor method, which is executed before the object is deleted when the object is deleted, is mainly used to close the system resources opened in the object

class A:
     def __getitem__(self, item):
         print(item)
a = A()

__getitem__ The new style of object[] adds, deletes, modifies and checks the object

__setitem__

__delitem__

__delattr__ del obj.attr is used to customize the method of deleting an attribute

 

There is only one object, only one memory space is opened

Create a class object property in the singleton pattern programming static properties in the class, all methods become class methods

Design Patterns---java

The singleton pattern in python is to use __new__

 

python

Displays all properties starting with a in an object

Second, the module

py files are modules

The reason why python is easy to use, there are many modules

three

  1. Built-in modules

    It comes with python installation

  2. Expansion module

    E.g:

      itchat

      beautiful soap

      selenium web automation testing tool

      django tornado 

  3. Custom modules

Built-in modules

serialization module

hashlib module

 

serialization module

What can be stored in a file must be a string, or a byte

Only bytes can be transmitted over the network

dic = {"layers":(90,23)}
print (str (dic))
print(str(dic).encode(encoding='utf-8')) 
##Note that the above Chinese will display the following \xe5........

  

dic = {"layers":(90,23)}
print (str (dic))
print(str(dic).encode(encoding='utf-8'))
b"{'\xe5\xb1\x82\xe6\x95\xb0': (90, 23)}"
s = b"{'\xe5\xb1\x82\xe6\x95\xb0': (90, 23)}"
res = s.decode(encoding='utf-8')
print(res)

  

# dic --> string serialization 
#string --> dic deserialization
#serialization==create a sequence=="create a string
#instantiate==create an instance
import json
#Serialization
dic = {"john":(190,90)}
ret = json.dumps(dic,ensure_ascii=False)
print(type(dic),dic)
print (type), right)
print('*', str(dic)) #Local method
# deserialize
res = json.loads(ret)
print(type(res),res)

  Execution output:

 

f = open('john','r',encoding='utf-8')
ret = json.load(f)
print (type), right)

  execute output

Dump serialization load deserialization is mainly used for a data directly stored in the file to deal with the file directly

Dumps serialization loads deserialization Only operates data in memory Mainly used for network transmission and dealing with multiple data and files

 

import pickle

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609262&siteId=291194637