Python quick tutorial (supplement 02): Python tips

Source: Internet
Author: User
Python quick tutorial (supplement 02): Python tips

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 __

When writing a Python library module, we often run some test statements. When this program is imported as a database, we do not need to run these test statements. One solution is to comment out the test statement in the module before import. Python has a better solution, that is, using _ name __.

Below is a simple library program TestLib. py. When TestLib. py is run directly, __name _ is "_ main __". If it is imported, __name _ is "TestLib ".

Python

1

2

3

4

5

6

7

8

9

Def lib_func ():

Return a + 10

Def lib_func_another (B ):

Return B + 20

If _ name _ = '_ main __':

Test = 101.

PRint (lib_func (test ))

Import TestLib above in user. py.

Python

1

2

Import TestLib

Print (TestLib. lib_func (120 ))

You can try not to use if _ name __= '_ main _' in TestLib. py and compare the running result.

2) more import usage

Import TestLib as test # reference the TestLib module and rename it to t

For example:

For example:

Python

1

2

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:

Python

1

2

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:

Python

1

2

From TestLib import *

Print (lib_func (120 ))

Query

1) query function parameters

When you want to know which parameters a function will receive, you can use the following method to query.

Python

1

2

Import inspect

Print (inspect. getargspec (func ))

2) query the attributes of an object

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:

Python

1

2

A = [1, 2, 3]

Print (hasattr (a, 'append '))

2) query the class and class name of the object

Python

1

2

3

A = [1, 2, 3]

Print a. _ class __

Print a. _ class _. _ name __

3) query the parent class

We can use the _ base _ attribute to query the parent class of a class:

Cls. _ base __

For example:

Python

1

Print (list. _ base __)

Use Chinese (and other non-ASCII codes)

Add # coding = utf8 in the first line of the Python program, for example:

Python

1

2

# Coding = utf8

Print ("How are you? ")

You can also use the following methods:

Python

1

2

#-*-Coding: UTF-8 -*-

Print ("How are you? ")

Indicates binary, octal, and hexadecimal numbers.

In version 2.6 or later

Python

1

2

3

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:

Python

1

2

3

Print (int ("1110", 2 ))

Print (int ("10", 8 ))

Print (int ("2A", 16 ))

Note

The comment in a line can start #

Comments with multiple rows can start with "'and end with"', for example

Python

1

2

3

4

5

6

7

8

9

10

'''

This is demo

'''

Def func ():

# Print something

Print ("Hello world! ") # Use print () function

# Main

Func ()

The comment should be aligned with the program block.

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:

Python

1

2

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.

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

Install non-standard packages

Python standard library is installed with Python. When we need a non-standard package, we need to install it first.

1) use linux repository (Linux environment)

This is a good starting point for installing Python additional packages. You can search for possible Python packages in Linux repository (for example, search for matplot in Ubuntu Software Center ).

2) use pip. Pip is a package management program that comes with Python. it connects to Python repository and searches for possible packages.

For example, use the following method to install, uninstall, or upgrade web. py:

$ Pip install web. py

$ Pip uninstall web. py

$ Pip install-upgrade web. py

If your Python is installed in a non-standard path (using $ which python to confirm the path of the python executable file), such as/home/vamei/util/python/bin, you can use the following method to set the path of the pip installation package:

$ Pip install-option = "-prefix =/home/vamei/util/" web. py

3) compile from source code

If the above method cannot find the library you want, you may need to compile from the source code. Google is often the best starting point.


The above is the Python quick tutorial (supplement 02): Python tips. For more information, see PHP Chinese website (www.php1.cn )!

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.