Python Training chapter III, module

Source: Internet
Author: User
Tags stdin

System-related Information module: Import sys
SYS.ARGV is a list that contains all the command-line arguments.
Sys.stdout Sys.stdin Sys.stderr respectively represents the standard input output, the error output of the file object.
Sys.stdin.readline () reads a line from the standard input sys.stdout.write ("a") screen output a
Sys.exit (Exit_code) Exit program
Sys.modules is a dictionary that represents all the available module in the system
Sys.platform getting running operating system environment
Sys.path is a list that indicates all paths to find Module,package.

Operating system-related calls and actions: Import OS
Os.environ A dictionary containing environment variables
os.environ["Home"] can get the value of the environment variable HOME
Os.chdir ( DIR) Change the current directory Os.chdir (' D:\outlook ')
Note Windows uses to escape
OS.GETCWD () get the current directory
Os.getegid () get the valid group ID os.getgid () get the group ID
Os.getuid () Gets the user ID Os.geteuid () gets a valid user ID
Os.setegid os.setegid () os.seteuid () Os.setuid ()
Os.getgruops () Get user group Name list
Os.getlogin () get user login name
os.getenv get environment variable
OS.PUTENV SET environment variable
os.umask settings umask
Os.system (cmd) Use system calls to run the cmd command

Built-in modules (can be used directly without import) commonly used built-in functions:
Help (obj) online, obj is any type
Callable (obj) to see if an obj can be called like a function
Repr (obj) obtains a representation string of obj that can be used to reconstruct a copy of the object using the string eval
Eval_r (str) represents a valid Python expression that returns the expression
Dir (obj) to view the name visible in the name space of obj
Hasattr (obj,name) See if there is a name in the name space of obj
GetAttr (obj,name) gets a name in the name space of obj
SetAttr (Obj,name,value) is the name of an obj
A name in space points to Vale, the object
Delattr (obj,name) removes a name from the name space of obj
VARs (obj) returns the name space of an object. expressed in Dictionary
Locals () returns a local name space, denoted by dictionary
Globals () returns a global name space, denoted by dictionary
Type (obj) to view the types of an obj
Isinstance (OBJ,CLS) to see if obj is a CLS instance
Issubclass (subcls,supcls) View Subcls is not a subclass of Supcls

################## type Conversion ##################

Chr (i) to turn an ASCII value into a character
Ord (i) turns a character or Unicode character into an ASCII value
Oct (x) turns integer x into a string represented by octal
Hex (x) turns integer x into a hexadecimal-represented string
STR (obj) gets the string description of obj
List (seq) converts a sequence into a list
Tuple (seq) converts a sequence into a tuple
Dict (), Dict (list) converted to a dictionary
int (x) is converted to an integer
Long (x) converts to a long interger
Float (x) converted to a floating-point number
Complex (x) converted to plural
Max (...) to find the maximum value
Min (...) to find the minimum value

Airmin

? Airmin

Import and from ... import module variables, method reference differences

or the module support.py in the above example:
def print_func (PAR):
Print "Hello:", par
Return

The correct way to introduce and invoke the Support module using import:
#!/usr/bin/python

- -Coding:utf-8--Import Module

Import support

You can now invoke the functions contained in the module.

Support.print_func ("Runoob")

Tip: It is not possible to implement calls directly using Print_func (), you must use the introduced module name as an object and call the method Print_func under the module object before invoking it.

The correct way to use the From ... import module:
#!/usr/bin/python

- -Coding:utf-8--Import Module

From support import *

You can now invoke the functions contained in the module.

Print_func ("Runoob")

Tip: You can implement calls directly using Print_func ().

I recommend that you generally use import statements to avoid using the from ... import because it makes your program easier to read and avoids name collisions.

Xiphap

? Xiphap

? xip*** @qq. com
2 months ago (03-01)
?

? Little White Learn Python

? 179***[email protected]

After reading this chapter on this from...import * statement and import difference is very confused read the explanation from elsewhere to understand the following.

First you need to understand the difference between import and From...import.
? Import module: Importing a module; Note: the equivalent of importing a folder is a relative path.
? From...import: Import a function in a module; note: the equivalent of importing a file in a folder is an absolute path.
?

So the difference in usage is when referencing a file:
Import//module. function

From...import//Direct use of function names is possible.

So

From...import *: is to import all the functions in a module; Note: Equivalent to: Imports all files in a folder, all functions are absolute paths.

Conclusion:

The From...import * statement differs from import in that:

Import imports the module, each time using the function in the module will be what module.

From...import * Import the module, each time using the function in the module, directly using the function can be; note because the function is already known to be in that module.

Python Training chapter III, module

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.