Python Learning notes (12-14)

Source: Internet
Author: User
Tags vars

12. Module

Sys.path Display environment variables.

Dir () all objects
Globals () Details of the global object
Locals () details of the local object
Used in the global environment, 2 is no different, when used in sub-functions, preceded by print or assignment. In fact, there is no print in the sub-function is the return value is not printed. Also, if you don't use command-line mode, the return value or variable is not printed in the global environment.

__name__== ' __main__ ' of the Code area
So you can use this in the module to prevent the import of the time automatically run.

Reload re-import the module that has been loaded successfully (without being imported will be an error), its role is only to modify the module, let the module reload, equivalent to Del,import. Because import does not check whether the module is updated, if the discovery already exists, even if the call is not loaded again.

Sys.modules shows the objects that have been loaded, which are not sys.xxx, but are directly used modules and methods, in the form of a dictionary, you can use the keys () values () to obtain a separate, generally with Sys.modules.keys () on the line.

Importing from OS import XXX separately
Prevent an item in the module from importing from OS import _xxx

--------------------------------------------------------------------------------------


Class 13

Each method has a parameter of self, which is equivalent to the this in C + +
__init__ equivalent to a constructor function
__del__ equivalent to a destructor
Methods for creating subclasses class CLS (the name of the parent class):
Can be multiple inheritance, if there is the same variable or method, the first parent class is the main.
If the child class overrides a method of the parent class, the parent class method is called with the parents. Methods (instances of subclasses, ...). Use an instance of a subclass as the first argument. If it is a constructor, write in the constructor of the subclass: parent class. __init__ (self,......)

If you print self in __init__. Then the 16-binary ID is printed, and the ID (the instance of the class) gets the same ID.
The difference between Dir (Class) and Dir (instance) is that the latter has all of the variables, which only have variables outside of the method in the class (defined outside the method, without self, static variables) and variables that are added to the class (such as: class. property = 123).
There is only __name__,__doc__,__module__ in Dir (). But __dict__ can still be used.

Class cls:arg=100 is quite similar to a static member variable defined in C + +. Note Use static members in the method, with "class. Variable name".
Python does not support static member functions and, if necessary, uses a global function to circumvent this limitation. is to define a global function to modify the static member variables of this class, which is not a member function of the class.

This paragraph is out of date, I was looking at the book is old, plus @ can use static member function, see:
Http://hi.baidu.com/xzq2000/blog/item/91cd8302ed15c4074afb5164.html

Name of the cls.__name__ class
Documents for the CLS.__DOC__ class
Properties of the Cls.__dict__ class
The parent class of the Cls.__bases__ class//This can only be used for classes and cannot be used on instances
CLS.__MODULE__ modules defined on a class

Issubclass (Sub,sup) determines whether the sub is a subclass of the SUP. It's weird to have to wrap up a bracket to print.
Isinstance (OBJ1,CLASS1) Obj1 is an instance of Class1
You can also use a type object, such as: Isinstance (4,type (4))

Hasattr (obj1,attr) checks if an object has a property
GetAttr (obj1,attr) Gets the value of the object's properties
SetAttr (Obj1,attr,set) Sets the value of an object's properties
Delattr (obj1,attr) Removes an object's properties

VARs (OBJ1) is equivalent to C=c () c.__dict__ to display an instance's own new attributes and corresponding values in the form of a dictionary
Example: >>>class C: Ver=1 >>>c=c () >>vars (c) {} >>>c.foo=1 >>>vars (c) {' Foo ' : 100}
VARs (Class) shows a variable that is consistent with Dir (class), but VARs (instance) shows fewer variables than Dir (instance), only the variables defined within the method (that is, self.xx) and instances. The variables defined by xx= ' 123 '.

There are also functions that can support some operations if implemented.
If you implement Def __str__ (self): return str (SELF.DATA)
When you print an instance of this class, the value of data is printed.
In addition, the print self in __init__ will also be called to this.
If you implement Def __len__ (self): return 123
When you perform a Len operation on an instance of this class, you get 123.
If there is no implementation, then the operation is either unsatisfactory or error-prone.
If you implement __add__, you can perform a + operation on the instance.
Refer to section 13.13, page 327 for details.


Precede the class property with __ to turn it into a private variable.

The subclass overloads the function of the parent class, then the child class calls the parent class method if the same name function is called, then the subclass is actually executed. Be careful.

--------------------------------------------------------------------------------------


14. Environment


Dir (__builtins__) can see all the built-in functions.

Callable determines whether an object type can be invoked by all (), that is, to determine whether it is a function or a class.

Compile create a segment code, the first parameter is the code, the second is the object to save the code, generally empty.
The last parameter has 3 values: eval is an evaluation expression, singel an executable statement, and exec a group of executable statements.
>>>eval_code=compile (' 100+200 ', ' ', ' eval ')
>>eval (Eval_code)
300

>>>single_code=compile (' print ' hello! "', ' ', ' Singel ')
>>>exec Singel_code
Hello!

>>>exec_code=compile ("" "
... xxxx
... xxxxx
... xx
', ', ', ' exec ')
>>>exec Exec_code

Eval evaluates a string of an expression, such as:
>>>eval (' 100+200 ')
300

Exec can also execute code in a py file, such as:
>>>f=open (' test.py ')
>>>exec F
Executes the program and points the file pointer to the end of the file.
Equivalent to execfile (' test.py ') can specify namespace execfile (filename,globals=globals (), Locals=locals ())

Input is equivalent to eval (raw_input ()) But there are differences, and input can accept the conversion of a string containing a letter to a tuple, but the Eval accepts the letter by mistake.
Example: >>>alist=input (' Enter: ')
123, ' AA '
>>>alist
(123, ' AA ')


Intern (str) adds a string to the memory, and later assigns the same value to the same reference ID
ID (' 124 ') a1= ' 124 ' with different IDs
Intern (' 123 ') a2= ' 123 ' with the same ID

Os.system (' Date ') can execute command-line commands for the operating system and enter the results displayed on the command line.
Note that Os.system does not return a value, and if it is good to get the return value, you should use Os.popen
F=os.popen (' ls ');d ata=f.readline (); F.close;print data

The OS module has a number of commands to operate the operating system, which can be queried for help files.

The res=os.fork is only suitable for Linux, and the return is 0, which indicates that it is a child process and returns non 0, which represents the parent process.

Sys.exit (Statis=0), throws an Systemexit exception, and exits without a capture.
In some versions, this function is initially included in the inline function and can be used directly.

The difference between sys._exit () and sys.exit is that this function does not do any cleanup work and immediately quits Python


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1766838

Python Learning notes (12-14)

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.