Python tips for using

Source: Internet
Author: User
1. Import Module
The import declaration is often used in Python to use objects defined in other modules (that is, other. py files).

(1). Write test statements using __name__
The following is a simple library program testlib.py. When running testlib.py directly, __name__ is "__main__". If it is import, __name__ is "TestLib".

Def Lib_func (a):
Return a + 10
def lib_func_another (b):
Return B + 20
if __name__ = = ' __main__ ':
Test = 101
Print (Lib_func (test))
print ' __name__: ', __name__ #注意观察

In user.py, import the testlib above:
Import TestLib
Print (Testlib.lib_func (120))

(2), more import use mode
Import TestLib as test # referencing the TestLib module and renaming it to T
Like what:
Import TestLib as T
Print (T.lib_func (120))

From TestLib import Lib_func # References only Lib_func objects in TestLib and skips TestLib reference fields
The benefit of this is to reduce the memory footprint of the referenced module.
Like what:
From TestLib import Lib_func
Print (Lib_func (120))

From TESTLIB Import * # References all objects in TestLib and skips TestLib reference fields
Like what:
From TestLib Import *
Print (Lib_func (120))

2. Enquiry
(1), Parameters of query function
When we want to know what parameters a function will receive, you can use the following method to query.
Import Inspect
Print (Inspect.signature (func))

(2), querying the properties of an object
In addition to using DIR () to query the properties of an object, we can use the following built-in (built-in) function to confirm whether an object has a property:
Hasattr (obj, attr_name) # Attr_name is a string
For example:
A = [A]
Print (Hasattr (A, ' append '))

(3), the class and class name to which the query object belongs
A = [1, 2, 3]
Print a.__class__
Print a.__class__.__name__

(4), querying the parent class
We can use the __base__ property to query the parent class of a class:
cls.__base__
For example:
Print (list.__base__)

3. Use Chinese (and other non-ASCII codes)
In the first line of the Python program, add #coding=utf8, for example:
#coding =utf8
Print ("How are You?") ")

can also be used in the following ways:
#-*-Coding:utf-8-*-
Print ("How are You?") ")

4, 2 binary, 8 binary and 16 binary digits
At 2.6 or later, the following is the way:
Print (0b1110) # Binary, starting with 0b
Print (0O10) # octal, beginning with 0o
Print (0x2A) # hex, starting with 0x

If this is an earlier version, you can use the following method:

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

5. Search Path
When we import, Python looks for the module in the search path. For example, import TestLib above requires testlib.py in the search path.

We can see the search path in the following ways:
Import Sys
Print (Sys.path)

We can add or remove elements from the Sys.path while Python is running. On the other hand, we can add a search path to Python by adding pythonpath environment variables to the shell.
Below we add/home/vamei/mylib to the search path:
$export pythonpath= $PYTHONPATH:/home/vamei/mylib
You can add the front line command to the ~/.BASHRC. In this way, we have changed the search path for a long time.

6. Script and command line combination
You can run a Python script using the following method, and go directly to the Python command line after the script has finished running. The advantage of this is that the object of the script is not emptied and can be called directly from 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.