Python basics and Attention points overview

Source: Internet
Author: User
Tags generator instance method shallow copy xpath cve

1. Only those parameters at the end of the formal parameter list can have default argument values
For example, Def func (A, b=1) is valid, but def func (A=1, b) is not valid.

2. The value of the default parameter is an immutable object, such as none, True, False, number, or string
For example, Def print_info (A, b = []): Is wrong

3. Callback function
Defined:
In general, applications often invoke the pre-prepared functions in the library through the API. But some library functions require the application to pass it a function first,
Be called at the right time to accomplish the target task. This incoming, later called function is called a callback function.

To make an analogy:
There is a hotel with wake-up service, but guests are asked to decide how to wake up. It could be a room phone call or a waiter to knock on the door.
Sleep is afraid to delay things, you can also ask to pour water basin on their head.
"Wake up" This behavior is provided by the hotel, equivalent to the library function,
The "wake-up" method is determined by the traveler and tells the hotel, namely the callback function;
And the traveler tells the hotel how to wake up their actions, that is, the callback function to transfer the function of the functions, called the registration callback function.

Instance:
http://blog.csdn.net/tchenjx/article/details/51661173

4. All variables can be understood as "references" to an object in memory
A mutable object can be changed once it is created but the address does not change, that is, the variable points to the original object, the immutable object opposite
strings, tuples, and numbers are immutable objects, while list, dict, set, etc. are objects that can be modified.

Value passing: The value of parameter A is passed in as a parameter, and the value of a is not changed when the program finishes running
Reference passing: Passes the address of parameter A, and the value of a is changed when the program finishes running

Python does not allow programmers to choose whether to pass a value or to pass a reference.
If the function accepts an immutable object, it is passed as a value;
If the function accepts a mutable object, it is passed as a reference.

anonymous functions
Lambda X,y:x+y
Filter (lambda x:x>3,[1,2,3,4,5])

Invert string text= "abcdef"
1) text[::-1]
2) Convert to list, with its reverse function


[: End:] and range (, end,) are output to end-1, such as:
text= "ABCdef"
Print Text[:5] #abcde

Blog:
https://changchen.me/about/
http://ssdxiao.github.io/Virtualization

A = [A]
b = [4,5,6]
zipped = Zip (A, B) # packaged as a list of tuples [(1, 4), (2, 5), (3, 6)]
Zip (*zipped) # In contrast to zip, can be understood as decompression, return two-dimensional matrix [(1, 2, 3), (4, 5, 6)]


function call problem (what the code below will output)
Https://www.cnblogs.com/zhangqigao/p/6397853.html Question 6
The 3rd time is based on the 1th function call, L = [0,1]


5. Instance method/class method/static method

6. Yield
Yield, generators, iterables

List derivation: MyList = [x*x for x in range (3)]

Generators are also one of the iterators, but you can only iterate them once.
The reason is simple because they are not all in memory, they are generated in memory only when they are called
Mygenerator = (x*x for x in range (3))
The difference between a generator and an iterator is to use () instead of [], and you cannot call the generator for the second time with the for I in Mygenerator

The use of yield is similar to the keyword return, which returns a generator
Yield you have to understand that when you call a function, the code in the function does not run. The function simply returns the generator object
Your code will run whenever the for statement iterates over the generator.


7. When the parameters of the function are indeterminate, you can use *args and **kwargs,*args without a key value, **kwargs has a key value.
http://blog.csdn.net/chenjinyu_tang/article/details/8136841


8. Crawler
1. Urllib2 Library, XPath
Gets the text inside the label
info = Bloger[0].xpath (' string (.) '). Encode (' Utf-8 '). Strip ()

File conversion to HTML format for analysis
From lxml import etree
Record = open ("test.html", ' R ')
Tree = etree. HTML (Record.read ())
desc = Tree.xpath ("//dt[text () = ' Bugzilla: ']") [0].getnext (). XPath (' string (.) ')
Pattern = Re.compile ("cve-\d+-\d+. *:")
Print Re.findall (r "(. *):", Pattern.findall (DESC) [0]) [0]


2. Excel
Import XLWT
Workbook = xlwt. Workbook ()
Sheet = workbook.add_sheet (' sheet ', cell_overwrite_ok=true)
Sheet.write (0, 0, ' Englishname ') # (row, column, write content)
Workbook.save (' Test.xls ')


3. Regular
Record = open (Suse_path+cve, "R"). Read () #文本内容转化为string
Pattern = Re.compile ("h4\>\n.*\n\Print Pattern.findall (record) [0]


9. The new class is breadth-first and the old class is depth-first

10. Shallow copy and deep copy
Direct assignment: The reference to the object is passed, the original list is changed, and the assigned B will make the same change.
Shallow copy: The sub-object is not copied, so the original data changes and the sub-object changes
Deep copy: All copies, how the original data changes and does not cause the copied element to change

11. Each object that is obtained from a sequence using for IS, in fact, copied from the original sequence.
So when this object is processed, only the replicator is processed, and the original sequence is not affected

Python Basics and Notes summary

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.