Common Python knowledge points

Source: Internet
Author: User
Tags sleep function

1. Type () can be used to view the object type. This is introspection. That is, you can see what type you are. This function is very useful in dynamic languages.

2. getattr function: This is a very useful function. It can obtain and call attributes and methods from modules, classes, and object instances based on strings. This function is very similar to the function pointer in C language and the member function pointer in C ++.

3. Use of character encoding

Encode converts Unicode to STR, and decode converts a string to Unicode. Therefore, to convert a string to another format, you can:

S = 'Chinese'

S. Decode (fromcodec). encode (tocodec)

You can also directly use: S. encode (tocodec ). In this case, decode is called by default and the default encoding method is used.

4. Python process display problems

The process name in the script language is shown as: Python. If there are multiple processes on a server, it is difficult to find out which program the process is. You can use third-party open-source libraries to solve this problem: setproctitle.

From setproctitle ImportSetproctitle,Getproctitle

Print ('Current process name: % s'% Getproctitle ())

Setproctitle ('Bucket')

Print ('Process name after setting: % s'% Getproctitle ())

5. byte usage

1). ord: can be seen in the conversion of characters to int type values.

2), CHR: ord direction operation. You can see that int type values are converted to characters.

6. Use of Time

1) The time. Sleep () function has the sleep function in C, measured in seconds, but can accept floating point numbers. This can represent milliseconds.

2) Ti = datetime. datetime. Now () can display the current time, including the current microseconds. The difference between the two values can indicate the interval: microsecondlong = timelong. Seconds * 1000000 + timelong. microseconds. The difference is between seconds and microseconds.

 

7. Use of OO

1) if you do not want the member variables or methods to be used externally (that is, the private feature), you can activate them with _ double underscores.

2) Python can also implement abstract base classes, that is, interfaces:

3) The _ STR _ attribute can convert an object into a string, that is, calling print (object) is a printed string.

4) _ call _ (MAGIC method) can call an object as a function. You can give it an input parameter. :

Def_ Call __(Self, Protover ):

Return API. protomodules [protover]. timeticks (

(Time. Time ()-Self. Birthday) x 100

)

8. coroutine

Coroutine is a function that can suspend, reply, and have multiple entry points.

 

 

 

 

Related Article

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.