Python Full stack learning-object-oriented advanced 3

Source: Internet
Author: User

Review:

#反射 must be able to read, must know where to use

#hasattr getattr setattr delattr

Built-in methods must be able to read and use as much as possible

__len__len (obj) results depend 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__ (), and the hash value of the settlement object

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

The results of __str__ str (obj) print (obj) '%s '%obj depend on __str__ for output, display

The results of __repr__ repr (obj) '%r '%obj depend 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 the object formatted

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

__new__ constructs the method, executes before executes the __init__, is responsible for creates an object, has the concrete application in the singleton pattern

__del__ destructor, which is executed before the object is deleted, and is used primarily to close system resources opened in the object

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

__getitem__ Object [] The new object to be added and censored

__setitem__

__delitem__

__delattr__ del obj.attr method for deleting a property from a definition

Only one object, only one memory space is opened

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

Design Patterns---Java

The singleton pattern in Python is using the __new__

Python

Displays properties that start with all a in an object

Two, module

The py file is the module

Python is easy to use, with many modules

Three kinds

1. Built-in Modules

When Python is installed, it comes with a

2. Expansion module

For example:

Itchat

Beautiful Soap

Selenium Web Automation testing tools

Django Tornado

3. Custom Modules

Built-in Modules

Serialization module

Hashlib Module

Serialization module

Can be stored in a file must be a string, or a byte

Only bytes that can be transmitted over the network

DIC = {"Number of layers":(90,23)}print (str (DIC)) print (str (DIC). Encode (encoding= ' utf-8 '))
# #注意上面中文才会显示下方 \xe5 ...

  

DIC = {"Layer":(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 ': ((+)} "res = S.decode (encoding= ' Utf-8 ') print (res)

  

# dic---string  serialization
#字符串 --dic deserialization
#序列化 = = Create a sequence = = "Create a string
#实例化 = = Create an instance
Import json# Serialization of DiC = {"John":(190,90)}ret = Json.dumps (dic,ensure_ascii=false) print (Type (DIC), DIC) print (ret), RET) Print (' * ', str (DIC)) #土办法 # deserialization res = json.loads (ret) print (Type (res), RES)

Execution output:

f = open (' John ', ' R ', encoding= ' utf-8 ') ret = Json.load (f) Print (type (ret), ret)

Execution output

Dump serialization load deserialization is primarily used for direct file handling of a data directly in a file

Dumps serialization loads deserialization only in-memory operation data primarily for network transmissions and multiple data-to-file interactions

Import Pickle

Python Full stack learning-object-oriented advanced 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.