Reading notes-The second edition of the basic Python tutorial-tenth chapter charging time

Source: Internet
Author: User
Tags sin pprint

10th Charge Time

10.1 Modules

Math Cmath

>>> Import math>>> math.sin (0) 0.0


10.1.1 Module is a program


Cat hello.py

#!/usr/bin/env python#coding=utf-8print "Hello, World"
>>> Import math>>> math.sin (0) 0.0
>>> Import sys>>> sys.path.append ('/root/python ') #或者sys. Path.expanduser (' ~/python ') >>> Import Hellohello, World


The PYc file will be generated when the module is imported, and the import module will import the. pyc file the next time it is imported, which is used for definition, such as variables, functions, classes , etc., if the module is imported, it will not be imported again.

Even if you execute the import command, you can avoid looping the import unless you use reload ()

>>> Reload (hello) hello, world<module ' hello ' from '/root/python/hello.pyc ' >

However, it is generally not recommended to use Relaod


The 10.1.2 module is used to define

The module can maintain its own scope

1. Defining functions in the Load module

Cat hello2.py #!/usr/bin/env python#coding=utf-8def hello (): print "hello,world!"

>>> Import Hello2

This means that the Hello function is defined within the scope of the module and can be accessed in the following way


>>> Hello2.hello ()

hello,world!


We can use the same method as the name defined in the global scope of any module

To make the code reusable, modularize it


2. Add the test code to the module


>>> __name__

' __main__ '


>>> hello2.__name__

' Hello2 '



# cat Hello3.py#!/usr/bin/env python#coding=utf-8def Hello (): print "hello,world!" # Testhello () >>> import hello3hello,world!





The purpose of adding __name__== ' __main__ ' is to have the module not execute when it is imported


# cat Hello4.py#!/usr/bin/env python#coding=utf-8def Hello (): print "hello,world!" def test (): Hello () if __name__== ' __main__ ': Test ()


>>> import hello4>>> Hello4.hello () hello,world!>>> hello4.test () hello,world!


10.1.3 Make your modules available

1. Place the module in the correct position

>>> Import sys,pprint>>> pprint.pprint (Sys.path) [', '/usr/lib64/python27.zip ', '/usr/lib64/ python2.7 ', '/usr/lib64/python2.7/plat-linux2 ', '/usr/lib64/python2.7/lib-tk ', '/usr/lib64/python2.7/lib-old ', '/ Usr/lib64/python2.7/lib-dynload ', '/usr/lib64/python2.7/site-packages ', '/usr/lib64/python2.7/site-packages/ gtk-2.0 ', '/usr/lib/python2.7/site-packages ', '/root/python ']

/usr/lib64/python2.7/site-packages is the best choice.

2, tell the compiler where to find

Edit Sys.path


Export pythonpath= "/library/python/2.7/site-packages:{$PYTHONPATH}"


Path configuration file. pth file to implement

Python in the process of traversing a known library file directory, if a. pth file is seen, the path recorded in the file is added to the Sys.path setting, and the. pth file says that the specified library can also be found by the Python runtime environment.

. pth file: Import is expected to be executed, #会被忽略


10.1.4 Bag


This article from "Small Fish Blog" blog, declined reprint!

Reading notes-The second edition of the basic Python tutorial-tenth chapter charging time

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.