Python Learning Notes (iv)

Source: Internet
Author: User

25. three quotation marks

Python uses three quotation marks to refer to large pieces of text, and can write any single double quotation mark in the middle

26. Original Character

when there are too many symbols in a string that need to be escaped, the original character can be used to avoid always escaping print (r "This is a newline simbol:\n.and, which is a simbol:*") as is shown in double quotes.

27. python introspection

Dir ( object )--- can see what methods and properties are available for this object

Dir (__builtins__) See what built-in functions

Help ( command )--- See how commands are used

print (logging.__file__)---- view The location of the logging module

28. parameters for print

Print (value1,... , sep= ', end= ' \ n ', File=sys.stdout,flush=false)

By default, the space between the parameters is split, the last parameter wraps, and if there are other printing requirements, you can modify the parameters.

29. Print out comment text

def PRINT_ABC:

"" prints out the ABC string "" "

Print ("abc")

Print (print_abc.__doc__)

Description: The description document for a function or class, which must be behind a line defined by a function or class, and before the first statement.

30. Line connector " \ "

when you output a string, but the string is too long to wrap in the code, but you cannot wrap the line in the display, use the row connector "\"

Print (" This is a very beautiful story,\

The hostess of the story is very beautiful. ”)

31. static methods, class methods

To create a static method:

@staticmethod

def method_name ()

To create a class method:

@Classmethod

def method_name ()

Static methods and class methods can be called by both classes and by instances of the class.

An instance method can only be called by an instance of a class and cannot be called by a class.

Class Name:

@staticmethod

Def name1 ():

Print ("Name1")

@classmethod

Def name2 ():

Print ("Name2")

Def name3 ():

Print ("Name3")

Name.name3 () # call Error

Name (). Name3 () # instantiation of the call, correct

Variable of class, variable of instance

Python 's class variables are different from C + + static variables and are not shared by all objects of the class. The class itself has its own class variable (stored in memory), and when an object of the TestClass class is constructed, a copy of the current class variable is given to the object, the value of the current class variable, and the value of the class variable that the object is copied from, and Modifying a class variable through an object does not affect the value of the class variable of other objects, because everyone has its own copy, and it does not affect the value of the class variable owned by the class itself; Only classes can change the value of class variables owned by the class itself.

Class Name:

Test = 1

def __init__ (self):

Self.test1 = 2

A = Name

b = Name ()

print (A.test, b.test, B.test1) # if a.test1 the error

A class can access a variable of a class and cannot access an instance's variable. Instances can access variables and instances of the class.

36. Inheritance and invocation

Class Parent:

def __init__ (self):

Self.name = ' name '

Class Child (Parent):

name1 = ' name1 '

def __init__ (self):

self.name2 = ' name2 '

def print1 (self):

print (self.name1) # is correct because there is no member variable name1in the init function, Self a variable that points to a class name1 . If the variables and member variables of the class have the same name1, point to the member variable name1.

Class Child (Parent):

Name3 = ' Name3 '

def print1 (self):

Print (Self.name3) # is correct, there are no variables defined for the instance. Self points to a variable of the class.

print (self.name1) # Correct, there is no member variable defined for the class, self points to the member variable of the parent class

PNS, execute system command

(1)import OS

Os.system ("command")

run system commands on only one sub-terminal and cannot get return information after command execution

(2) Os.popen ()

TMP = Os.popen (' ls *.sh '). ReadLines ()

This method not only executes the command but also returns the information object after execution. The advantage is that the returned result is assigned to a variable to facilitate the processing of the program

(3) using the module subprocess

Import subprocess

p = subprocess. Popen (' ls *.sh ', shell=true, stdout=subprocess. PIPE, Stderr=subprocess. STDOUT)

Print P.stdout.readlines ()

For line in P.stdout.readlines ():

Print line,

retval = P.wait ()

The advantage is that by using the control and monitoring of the thread, the returned result is assigned to a variable, which facilitates the processing of the program.

Subprocess is recommended when the arguments for the command are executed or the return contains Chinese text ,and if you use os.popen , an error will occur .

Python Learning Notes (iv)

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.