There are mountains outside the python--mountains that I've been through all those years (fourth chapter)

Source: Internet
Author: User
Tags iterable new set

Adorner:


Defined:

Essentially a function, (decorating other functions) is adding additional functionality to other functions
Principle:
1. Cannot modify the source code of the decorated function
2. Cannot modify the calling mode of the decorated function

Advantages:
The most intuitive benefit of adorners is the reduced refinement of functions and the addition of new features in bulk .


Knowledge required to implement adorners:
1, function is "variable"

the principle of a function is similar to a variable reference in that it maps a name to a Memory object  
2. High-order function
a higher-order function satisfies one of the following two conditions:
A, passing a function name as an argument to another function (adding functionality to it without modifying the code of the decorated function)
B, one function return value contains another function name (does not modify the function's invocation method)
3. Nested functions
an attribute with local variables, that is, to continue using DEF to define new sub-functions within the function;
4. Higher order function + nested function = = Adorner

The simplest example of adorners:

1 def Auth (_func): 2     def Decorate (*args, * *Kwargs):3         < requires additional features >4         result = _func (*args , * *Kwargs)5         < required additional functions >6         return  result 7 8     return Decorate
View Code

Python's memory recovery mechanism:
 1. When a memory object does not have a variable reference, the memory object is recycled over a period of time
2, cancel the variable reference can use the DEL keyword, in fact, the implementation of the variable name and memory object mapping relationship between


Generator (Generator):
eg. (i**2 for I in range (10)) returns the result as a generator  
1. The corresponding data will be generated only at the time of the call
2, can only record the current location and data, and can only backward, not forward
3, the method to get the next data is called: __next__ () method

iterators vs Iterative objects (Iterator vs iterable)
1, iterators are objects that can call the next () method
2. An iterative object refers to an object that can use a for ...: Traversal
3. Iterative objects can be converted to iterators via the ITER () method
4, iterators must be iterative objects
5, but iterative objects are not necessarily iterators, can be converted via ITER ()

Use of anonymous functions:

  Anonymous functions, also known as: Lambda functions. This code shows how to use the lambda function:

Lambda x:2 * xprint f (3)

  A return statement is a lambda function that has not been used, and it always returns something. A lambda function can contain an IF statement:

Lambda x:x >print(f (2))print(f (12))
call Lambda in the map function:

  The definition of a map is a map (function, which can be iterated). It applies to a function that repeats every item. We can use map () to work with the lambda function in the list:

List = [1,2,3,4,5= map (lambda x:x*x, list)print(squaredlist)

  Using a lambda function anywhere, you can use a normal function instead. A lambda function is not a statement, it is an expression. A block of statements that the lambda function does not support.

call Lambda in the filter function:

  A filter (function, iterator) creates a new list of elements, and the function returns True. Example:

List = [1,2,3,4,5,6,7,8,9,10= filter (lambda x:x% 2 = = 0, list)print(newlist)

  The returned list returns a value that contains only the elements of the lambda expression "Lamba x:x% 2 = = 0" as true.

call lambda in the reduce function:

  The reduce function, reduce (function, iterable) applies to two parameters that accumulate to an iterative project, from left to right. Example:

List = [1,2,3,4,5= reduce (lambda x,y:x+y,  list)print(s)

  In this case, the expression is always: true, so it simply summarizes the elements of the list. Another example:

List = [10,6,7,5,2,1,8,5= reduce (lambdaifelse  y, list)print (s)

Built-in functions:

1. ABS: Return absolute value
2. All: Iterates through an iterator, if both are empty, or if each iteration element is true, returns ture, equivalent to:

def All (iterable):      for inch iterable:         if  not element:             return False     return


3, any: In contrast to the all function, if an element of any iteration is ture, or is empty, it returns false
4. ASCII: Turn into ASCII code
5, Bin: Turn an integer to get a binary string
6, BOOL: Convert a value to get a bool type
7. ByteArray: Returns a byte of data
8, Bytes: converted to byte form according to an encoding
9, Callable: callback function, if return False, represents a callback failure, if true, it may still fail
10. CHR: Returns the Unicode form of a shaped number, such as 97 returns a
11. Classmethod: Returns a class method for a function
12. Compile: Compile a resource into a code or an AST object
13. Complex: Create a complex expression
14. Delattr: Remove properties from an object
15. Dict: Create a dictionary structure (like map)
16, dir: If there are no parameters to return the path of this file, if there are parameters to return an object's property list
17, Divmod: Two number of shaping numbers to divide
18. Enumerate: Returns an enumeration object
19. Eval: Perform an expression, or string as an operation
20. Exec: Supports dynamic execution of Python code
21. Filter: Filters out the target string within a tuple
22. Float: string converted to floating-point type
23. Format: Formatted string
24. Frozenset: Returns a new cloned object
25. GetAttr: Gets the result of a method of the object, similar to the X.foobar
26. Globals: Returns the current global dictionary table
27, Hasattr: Determine if there is a property value, return True to indicate that there is
28, Hash: Take an object's hash value
29, Help: Call the system built-in assistance system
30. Hex: Converts an integer number to lowercase hexadecimal
31. ID: Returns the unique identity value of an object
32. Input: Reading data from the console
33, int, convert string int type
34, Isinstance: Determine whether an object is an instance of the class
35. Issubclass: Determine if a class is a subclass of another class
36. ITER: Returns an object that can be iterated
37. Len: Returns the length of a string
38. List: Print a Collection Object
39, locals: Finer and returns the local flag table of a dictionary
40. Map: Returns an iterative map function
41. Max: Returns the largest value or values in the collection
42. Memoryview: Returns the internal data of a Python object
43, min: Returns the smallest one or more values within the set
44. Next: Returns the next value in the set
45. Object: Returns a new object that is the parent class of all classes
46. Oct: Returns a shape of eight-binary type
47. Open: Opens a System file
48. Ord: Gets the ASCII value of a string or Unicode type
49. Pow: Returns the number of n-th square value
50. Print: Output statement
51. Property: Returns an attribute value
51. Range: Produces a sequence of numbers
52. Repr: Returns a String printable object
53. Reversed: Invert a collection
54. Round: Returns a rounded floating-point number
55. Set: Returns a new set object
56. SetAttr: Set a new property value
57. Slice: Returns the set of intervals for a collection
58. Sorted: Sort A collection
59, Staticmethod: Declaration Returns a static method
60. Str: Converts a numeric type to a string
61. Sum: Sum the values inside a set
62. Super: Returns an object of the surrogate parent class
63. Tuple: Returns an immutable tuple
64. Type: Returns the types of an object
65. VARs: Returns the properties of an object
66. Zip: Returns the combination of a peer item
67, __import__: Import Advanced method than import


Built-in constants inside Python:


1, the value of the False:bool type
2, the value of the True:bool type
3. None: null for Object
4, notimplemented: Not other types of implementation
5, Ellipsis: omitted
6. __debug__:debug option
7, Quit,exit,copyright,license,credits

Other small points of knowledge:


1. Get the absolute path:

Os.path.abspath ()

2. Get the parent directory

Os.path.dirname ()

3. Get the function name:

Func. __name__

Or

GetAttr (func,__name__)

There are mountains outside the python--mountains that I've been through all those years (fourth chapter)

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.