Python rookie Diary 2

Source: Internet
Author: User

1. For a subclass relationship between classes can be passed: for example, C1 is a subclass of C2, C2 is a subclass of C3, then C1 is also a subclass of C3, any class can be considered as its own subclass, class can also be nested inside a class


2. In a class, to refer to a class's properties (global variables) in a function, you must use the full name (the class name, the name of the variable), and for the statement in the class body, you must use the simple name of the property to refer to the property of the class.
Example: Class C ():
X=55
Y=X+66--You must use a simple name
def b ():
F=c.x**3--Full name must be used
Print (f)


3. Succession issues: Parsing order by traditional methods, and new method parsing order
For example: Class Base1:
def amethod (self):
Print (' Base1 ')
Class Base2 (BASE1):
Pass
Class Base3:
def amethod (self):
Print (' Base3 ')
Class Derived (BASE2,BASE3):
Pass
Aninstance=derived ()
Aninstance.amethod () #打印base1
First, the inheritance relationship is a tree, the traditional order is: first find Amethod () function, first derived class, then Base2, then Base1, then base3, until found
The New Order is: First find Amethod () function, first derived class, then Base2, then Base3, then Base1, until found, (Must each class contains a __mro__ attribute)


One of the principles in inheritance is that subclasses can override a parent class, and if a property or function in a subclass has the same name as a parent class, then the parent class's properties or functions are overwritten in the subclass. The reason is that all of the above access order is now accessed
Subclass to go to the parent class, if found in the subclass will not be found in the parent class


4. Under multiple diamond-based inheritance, we can consider using the Super.super (aclass,obj) built-in function to ensure that each ancestor's method (function) is called only once, which returns a special superclass of the object obj, the lookup order is the MRO order
For example: Class A:
def miss (self):
Print (' A.miss ')
Class B (a):
def miss (self):
Print (' B.miss ')
Super (B,self). Miss () # A.miss (self)
Class C (a):
def miss (self):
Print (' C.miss ')
Super (C,self). Miss () # A.miss (self)
Class D (b,c):
def miss (self):
Print (' D.miss ')
Super (D,self). Miss ()

A=d ()
A.miss ()
The result is: D.miss
B.miss
C.miss
A.miss


5. See similar c:\> is the command line mode provided in Windows, see >>> is in the Python interactive environment, the Python interpreter is used to execute the Python code you entered


6.Python, excluding/, rounding//, residual% >>> 10/3
3.3333333333333335
>>> 10//3
3
>>> 10%3
1


7. For a single character encoding, Python provides an integer representation of the Ord () function to get the character, and the Chr () function converts the encoding to the corresponding character:
Ord (' B ')
66
>>> Ord (' White ')
30333
Chr (33333)
Rudder
>>> chr (22222)
' 囎 '


8. It can be seen that 1 Chinese characters are UTF-8 encoded and usually consume 3 bytes, while 1 English characters take up only 1 bytes


9. Multivariable Output: >>>print (' This is%d%d's account his deposit amount is%s,%s '% (a,b,c,d))
>>> This is a 555666 account, and his deposit amount is aaa,bbb.
The variable after% corresponds to the previous one by one, and the order is identical


10. For tuples we use () parentheses to define, he is similar to the list, but he can not be modified, a= (1), if this defines a tuple with an element, this will produce a singular, that can be understood as the number 1, with a parenthesis,
It can also be understood as a tuple containing an element, so python specifies that a tuple containing an element should be added with a comma after the a= (1,).
However, the following Lezi group seems to be mutable:

>>> t = (' x ', ' Y ', [' P ', ' Q '])
>>> t[2][0] = ' P '
>>> t[2][1] = ' Q '
>>> T
(' x ', ' Y ', [' P ', ' Q '])
In fact, not, there is a list in the tuple, in fact, the tuple is not the element of the tuple, but the elements in the list of tuples, the list of elements can be changed this we all know, so, the above example is actually changed the list of elements

11. In the dictionary, a key corresponds to a value, if the key is assigned several times, then the back will flush out the previous value, only the last value, to determine whether a key has value corresponding to it, there are two ways
①,>>>key in Dict_name If key is in Dict, returns True, not false
②,>>>dict_name.get (key) calls the Get function to determine if the key is in the dictionary


12. Compared to list, Dict has the following features:


The speed of finding and inserting is very fast and will not increase with the increase of key;
It takes a lot of memory, and it wastes a lot of memory.
And the list is the opposite:


The time to find and insert increases as the element increases;
Small footprint and little wasted memory.
So, Dict is a way of exchanging space for time.


Dict can be used in many places where high-speed lookups are needed, almost everywhere in Python code, it is important to use dict correctly, and the first thing to keep in mind is that the Dict key must be an immutable object.


This is because Dict calculates the storage location of value based on key, and if each calculation of the same key results in a different result, the dict interior is completely chaotic. The algorithm for calculating the position by key is called the hash Algorithm (hash).


To ensure the correctness of the hash, the object as a key can not be changed. In Python, strings, integers, and so on are immutable, so you can safely use them as keys. The list is mutable and cannot be a key:


13.set and dict are similar, but also a set of keys, but do not store value, to create a set, you need to provide a list as an input collection: A=set ([1,2,3,4,5,6])
>>> a = set ([1, 2, 3,4,5,6])
>>> A
{1, 2, 3,4,5,6}--Note curly braces
Because set is a set of keys, it has the characteristics of the set, disorder and non-repetition, when there are duplicate values in the list, it will filter out duplicate values, because it is a set, so you can do the intersection and the operation of the set, A & B (intersection); a | B (also set)


14. The function name is actually a reference to a function object, it is possible to assign the function name to a variable, which is equivalent to giving the function an "alias": A=max then A is the alias of Max, and the usage is the same as Max.


15. Functions can return multiple values, when the function to return multiple values, in fact, the return is a tuple, in syntax, return a tuple can omit parentheses, multiple variables can receive a tuple at the same time, the corresponding value is assigned by location.
For example: def a (c,d):
F=c+d
g=c/d
H=c*d
Return f,g,h
X,y,z=a (6,3)
Print (x, y, z)
Print (A (6,3))

Results: 9 2.0 18
(9, 2.0, 18)


16. Variable parameters, due to the number of arguments are not determined, we first think we can put a,b,c ... As a list or tuple comes in, we need to assemble a table or tuple when we call it, but if we use mutable parameters, then python
When we call the function, we automatically assemble the value we passed into as a tuple def function_name (*number): When the value is called, it is directly function_name (,....) instead of being written function_name ([ ,...])
Keyword parameter, def function_name (**number), called when: function (a=1,b=2,c=3 ...), keyword argument passed in is a dictionary, similar to the above




17. The default parameter must be used immutable object, if it is a mutable object, the program will run with a logic error!


18. The recursive function is to call itself, in the function of the return to indicate that the recursion is over,

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python rookie Diary 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.