Tips for Using python

Source: Internet
Author: User
1. the import Module often uses the import declaration in Python to use the objects defined in other modules (that is, other. py files. (1) use _ name _ to write the test statement. Below is a simple library program TestLib. py. When TestLib. py is run directly, __name _ is & amp; quot ;__ main __& amp; quot ;. If it is imported,... 1. import Module
In Python, the import declaration is often used to use objects defined in other modules (that is, other. py files.

(1) use _ name _ to write test statements.
Below is a simple library program TestLib. py. When TestLib. py is run directly, __name _ is "_ main __". If it is imported, __name _ is "TestLib ".

Def lib_func ():
Return a + 10
Def lib_func_another (B ):
Return B + 20
If _ name _ = '_ main __':
Test = 101.
Print (lib_func (test ))
Print '_ name __:' ,__ name _ # observe

Import TestLib above in user. py:
Import TestLib
Print (TestLib. lib_func (120 ))

(2) more import usage
Import TestLib as test # reference the TestLib module and rename it to t
For example:
Import TestLib as t
Print (t. lib_func (120 ))

From TestLib import lib_func # only reference the lib_func object in TestLib, and skip the TestLib reference field
This reduces the memory usage of the referenced module.
For example:
From TestLib import lib_func
Print (lib_func (120 ))

From TestLib import * # reference all objects in TestLib and skip the TestLib reference field
For example:
From TestLib import *
Print (lib_func (120 ))

2. query
(1) query function parameters
When you want to know which parameters a function will receive, you can use the following method to query.
Import inspect
Print (inspect. signature (func ))

(2) query object attributes
In addition to using dir () to query object attributes, we can use the following built-in (built-in) function to check whether an object has a certain attribute:
Hasattr (obj, attr_name) # attr_name is a string
For example:
A = [1, 2, 3]
Print (hasattr (a, 'append '))

(3) query the class and class name of the object
A = [1, 2, 3]
Print a. _ class __
Print a. _ class _. _ name __

(4) query the parent class
We can use the _ base _ attribute to query the parent class of a class:
Cls. _ base __
For example:
Print (list. _ base __)

3. use Chinese (and other non-ASCII codes)
Add # coding = utf8 in the first line of the Python program, for example:
# Coding = utf8
Print ("How are you? ")

You can also use the following methods:
#-*-Coding: UTF-8 -*-
Print ("How are you? ")

4. it indicates binary, octal, and hexadecimal numbers.
In version 2.6 or later, it is represented as follows:
Print (0b1110) # Binary, starting with 0 B
Print (0o10) # octal, starting with 0o
Print (0x2A) # hexadecimal format, starting with 0x

For earlier versions, you can use the following method:

Print (int ("1110", 2 ))
Print (int ("10", 8 ))
Print (int ("2A", 16 ))

5. search path
During import, Python searches for modules in the search path ). For example, the above import TestLib requires TestLib. py to be in the search path.

You can view the search path using the following method:
Import sys
Print (sys. path)

You can add or delete elements in sys. path when running Python. On the other hand, we can add a search path for Python by adding the PYTHONPATH environment variable to the shell.
Next we will add/home/vamei/mylib to the search path:
$ Export PYTHONPATH = $ PYTHONPATH:/home/vamei/mylib
You can add the front line of commands ~ /. Bashrc. In this way, we will change the search path for a long time.

6. combine scripts with command lines
You can use the following method to run a Python script. after the script is run, you can directly enter the Python command line. The advantage of this is that the script object will not be cleared and can be called directly through the command line.
$ Python-I script. py

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.