Python Object-oriented three

Source: Internet
Author: User

Isinstance: Checks whether object is a class CLS object, followed by the creation of its own class, or it can be a base class. Can be used to determine if the value in Excel is of type int

Example: Isinstance (Object, Class)

Issubclass: Checks whether a class is a subclass of a class.

Example: Issubclass (class, Class)

Cases:

Class A:

Pass

Class B (A):

Pass

b = B ()

Print (Isinstance (b,a))-->true, judging B is created by a,

Print (Issubclass (b,a))-->true, judging class B inherits Class A,

Exception handling:

Exception Catch all: Exception

Code block: Try ... except exception,e

E is an instance, which is created by the exception class.

For example:

Try

inn = input ("Input num:")

print (INN)

Except Exception,e:

Print (e)

Exception-Detailed capture: Keyerror,indexerror ...

For example:

Try

K = [11,22,33]

K [100]

Except Indexerror as E:

Print (e)

Use the detailed catch error first, and then use the full to catch the error message.

Raise Exception ("content"): actively triggering an exception, encapsulating the content into E.

Try

# #逻辑代码

Pass

Except Indexerror,e:

#捕获异常

Pass

Except Exception,e:

# #捕获异常

Pass

Else

# # #逻辑块中未出现异常, do it here!

Pass

Finally

# # #不管有没有异常, do it all.

Pass

Available in code to connect to the database

The __str__ method in the class is called when the print object is

For example:

# # #利用面向对象来定义错误, and then call.

#定义错误类: Alexerror

Class Alexerror (Exception):

#使用构造方法, to the inside of the parameters, to achieve dynamic display error message effect

def __init__ (Self,messge=none):

Self.message = Messge

# #调用此方法, return error message

def __str__ (self):

If Self.message:

Return Self.message

Else

Return "Alex Error"

Try

# #触发错误

Raise Alexerror ()-no parameters are displayed if parameters are worn

Except Alexerror as E:

Print (e)--Show: "Alex Error"

ASSERT: Conditional statement, Judgment, 1, program test use. 2, if the conditions are met, the conditions can not be used.


Reflection:

Manipulating elements in a container in memory

GetAttr: Go to a container (module) to find the function, String function name. If there is, get the function

Hasattr: Determine if a function exists within a container.

SetAttr: Go to a container and set up a function.

Delattr: To remove a member from a container.

__import__ (parameter): Dynamic Import Module

For example:

Import Home

Controller,action = input ("Shuru URL:"). Split ("/")--split module and method

module = __import__ (Controller)--Dynamic Incoming module parameters

Func = GetAttr (module,action)--Find out if an action is in the incoming module

ret = func ()--Get return value

Print (ret)


Single-Case mode:

Only a single instance exists in memory

Create a static field to hold the instance that is always stored in memory.

Create a method that is used to store in memory.

Socket

Sockets

Create Socket:socket.socket ()

Send: Sends the data to the buffer, which is processed by the buffer.

Sendall: Sends data to buffer, immediately sends out, does not need buffer processing

Rev (1024): Size of the largest received data

Example:

Client:

# #导入socket

Import socket

# #创建socket

obj = Socket.socket ()

# # #创建连接, via 8341 ports

Obj.connect ((' localhost ', 8341))

# # #发送请求数据

Obj.send ("000000")

# #接受返回的数据, maximum size is 1024

Server_data = OBJ.RECV (1024)

Print (Server_data)

# # #关闭连接

Obj.close ()

Service side:

# #导入socket

Import socket

# #创建socket

Obj_server = Socket.socket ()

# # #监听端口

Obj_server.bind ((' localhost ', 8341))

# # #连接请求最大值

Obj_server.listen (5)

# # #循环发送数据

While True:

Print ("Waiting ....")

# # # #阻塞, waiting to connect

CONN,ADDR = Obj_server.accept ()

# # #接受客户端发过来的数据, maximum size is 1024

Client_data = CONN.RECV (1024)

Print (Client_data)

# #发送数据

Conn.send ("111111111")

# #断开与客户端的连接

Conn.close ()

Socketserver: A class must be created, the class must inherit Socketserver.baserequestionhandle, and a method must be defined handle

SELF.REQUEST.RECV ()


Python Object-oriented three

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.