Python Learning notes (2)

Source: Internet
Author: User

One, module and package

1. Namespaces

There are 3 types of namespaces, namely, the built-in namespace, the global namespace, and the local namespace. The built namespace is created when the Python interpreter starts, and the global namespace is created when the module is imported, and both will remain until the interpreter exits. A local namespace is created when a function is called, or is deleted when an exception is thrown.

2. Module

Organizes a set of related names, functions, classes, or their combinations into a file. A file is a method of organizing a module on a physical layer. A file is seen as a standalone module.

Import module:

Import Module1

Import Module2 or

Import Module1,module2

When the import module fails, the possible reason is that the module is not in the search path, resulting in the failure of the path search.

Import Sys

Sys.path.append (' Packet Storage path ')

or set the PYTHONPATH environment variable

You can also import specified functions or modules.

From module import name1,name2

If you need to import all the properties of the specified module into the current namespace, you can use the

From module Import *

You can use the import mymodule as mm to specify the local binding name.

Features of module import:

1) Load-on execution module

2) A module will only be loaded once, no matter how many times it is imported.

Second, abnormal

1. Try ... except statement

Try

< program code to execute >

Except exception Class 1 [, exception information variable]:

< code executed at the time of exception >

Except exception Class 2 [, exception information variable]:

< code executed at the time of exception >

2.try ... except...else statement

The program skips all except to execute the code in the ELSE clause when it is similar to 1, except that the try statement does not occur unexpectedly.

3.try ... except...finally statement

With 1, the statement in finally is definitely executed, regardless of whether an exception occurs.

4. Throw exception raise Exception class [, exception parameter]

5. Custom exception, must inherit exception or Baseexception

Class Userinfoerror (Exception):

def __init__ (self,code,message):

Self.code=code

Self.message=message

def __str__ (self):

Errorjsoninfo = ' {' Code ': '%d ', ' message ': '%s '} '% (Self.code,self.message)

Return Errorjsoninfo

Third, the document

1. Open, you can use the open or the file built-in function opens (filename,accessmode= ' R ', Buffering=-1), AccessMode represents the open mode, the optional parameters are ' R ', ' W ', ' A ' and so on. Buffering means buffering, 0 means no buffering, 1 means buffering only one row of data, greater than 1 represents a buffer size, and 0 means no buffering.

For example:

File1=open (' 1.jpg ', ' RB ')

2. Close the file using the close () function

3. Reading and writing

1) file reads, read (), ReadLine (), ReadLines ()

Read ([size]): There is an optional parameter, size, that represents the content of size length before reading. parameter does not specify or specify a negative value, all the contents of the file are read.

ReadLine ([size]): This size differs from the Read function, which refers to the length of the first size byte of the specified row that is read by the pointer.

ReadLines (): reads all the contents of the current position pointer at once, and the function returns a list of each row of data.

2) write, use the Write () function and the Writelines function

Write: Writes a string to a file

Writelines: Writes string contents from a list to a file

3) Location of files

Fileref.seek (offset,startpoint=0), startpoint with 0,1,2 instead, 0 means that the file starts, 1 is the current position, 2 means the end of the file, The default is 0.offset to indicate the number of bytes to be moved backwards by startpoint

Fileref.tell (): Gets the current position of the position pointer, relative to the beginning of the file offset.

Python Learning notes (2)

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.