Learn Python every day (4)

Source: Internet
Author: User

========================================================== ==========================================

 

========================================================== ==========================================

1. Import [convert] (http://www.cnblogs.com/dajianshi/archive/2012/12/28/2837270.html)

As we all know, python can either import a package or import a module. A package is generally defined as a multi-file module, if a directory contains "_ init __. python recognizes all the files under this directory as a package (this is a bit like Java's namespace, but Java does not need such a special file ), if this file is not available, Python considers the py files in the directory to be unrelated independent modules. However, you cannot do this in a subdirectory. If the subdirectory does not contain "_ init _. py", the program in this directory is written in white and there is no way to reference it. Of course, it is not absolute, unless you go around the big bend: set the current directory, import it, and reset the current directory. For example, the program needs to use the modules in the test directory, but the test directory is not a package. You can only do this:

   os.chdir("test")   import testfuncs   os.chdir("..")

The most convenient introduction is of course the module of the same directory (except for its simple programs, few will use this flat directory structure. For example, to use a function in another file module in the main program, you only need to import it directly:

Import testfuncs # directly import the module (that is, the file name without the extension) testfuncs. nousefunc () # reference the function from testfuncs import nousefunc # The function nousefunc () in the import module # directly call the imported Function

The above is only the simplest case. If the file defines a class, the actual usage is similar. However, you need to call the class instance one more time:

Import testclass # directly import the module (that is, the file name without the extension) OBJ = testclass. testclass (); # instantiate the class obj. func1 () # reference the class in the module name before the class function. Note that the class must be instantiated from testclass import testclass # class OBJ = testclass () in the import module (); # instantiate the class obj. func1 () # Call a function

If the package of the same directory or sub-directory is introduced from the program, the process is simple. However, if it is a module in the same sub-directory, the module or class in the brother directory should be introduced, what should I do? For example, the following directory structure:

In the figure above, the src directory is the top-level directory of the program and the top-level package for package import. The pub directory and Its subdirectories are the directory of the public program. In this case, the best method is in the main program (usually located in the root directory of the application's source program, in start. PY), and the sys. path, and then in the sub-directory module, you only need to use a fully qualified package name to introduce the modules or classes in other sub-directories. However, the actual situation is often less satisfactory. For example, a test program written for a public package needs to be placed in the pub/test directory, and the test target is under the pub/data directory, this is the main program that cannot be sent with the application, because it will not run the application at this time. In this case, the Startup Program and the referenced package are in the same sub-directory of the parent directory. In this case, we still need to add the parent directory (src/pub) and (src/pub/data, src/pub/test) to SYS. path, and then use

Import in an absolute way (import pub. Data. datautil/From pub. Data. datautil import datautil ). Every module is required to do this. Therefore, I specially wrote a function to automatically handle this situation:

Import OS, sysimport testclassimport testfuncs; from testclass import testclass; def _ preparerelativeimport (layer = 2): "prepares for relative reference so that the modules in the lower-level directory can, reference [local directory] and [parent directory] and [sibling Directory] modules. The parameter layer, indicating the number of parent directories introduced. The default value is 2-> introduce the local directory and parent directory; 3-> introduce the local directory, parent directory, and grandfather directory. "Import sys, OS; CURP = OS. path. abspath (OS. path. dirname (_ file _); orip = CURP ;__ package __= CURP. rpartition (OS. path. SEP) [2]; print ('\ r \ ncurdir =', CURP); While layer> = 0: layer-= 1; if not CURP in SYS. path: SYS. path. append (CURP); Pa = CURP. rpartition (OS. path. SEP); curn = pa [2]; pp = pa [0]; OS. chdir (PP); # If '_ init _ 'in ''. join (OS. listdir (CURP) :__ import _ (curn); CURP = pp; OS. chdir (orip); If _ name __= = '_ main __': If not '_ file _' in Dir () :__ file __= OS. path. abspath ('. ') + OS. path. SEP + "1.py"; _ preparerelativeimport (2) from testclass import testclass; From pub. test. testclass import testclass; From pub. data. compareoperator import compareoperators print ('\ r \ ntest of relativeimport done! ')

2. Input

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
from sys import argvscript, first, second, third = argvprint "The script is called:", scriptprint "Your first variable is:", firstprint "Your second variable is:", secondprint "Your third variable is:", third

Execute scripts and Output

$ python ex13.py first 2nd 3rdThe script is called: ex13.pyYour first variable is: firstYour second variable is: 2ndYour third variable is: 3rd

 

View Doc

Windows:

python -m pydoc raw_input

Linux:

pydoc raw_input

Also Input

>>>input("the meaning of life:")the meaning of life:2525

3.

print "helloworld!"raw_input("Press <enter>")

After adding the above Code to the last line, double-click the. py file to display the output result... I didn't expect it...

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.