Python Learning record Day4

Source: Internet
Author: User
Tags for in range ord

1. Built-in function supplement

callable (object)
Checks whether object objects can be called
1, the class is can be called
2. Instances cannot be invoked unless the __call__ method is declared in the class

def F1 ():     Print ("test""test"print(callable (F1)) Print (Callable (F2))
True
False

chr (i)
Returns the ASCII character corresponding to the integer i

Print (Chr (81))
Print (Chr (81))

ODR (c)
The parameter c is an ASCII character, and the return value is the corresponding decimal integer

Print (Ord ('b'))
Print (ord (' B '))

Random Verification Code

Import= [] for in range (5):    = Random.randrange (65,91 )    = chr (temp)    "". Join (LI)print(Result)

compile (source, filename, mode[, flags[, Dont_inherit]])
Compiles source into code or AST object. Code objects can be called by the EXEC statement ...
Compile (source, filename, mode[, flags[, Dont_inherit]])
英文说明: Compile source into code or AST object. The code object can be evaluated by EXEC statement execution or eval ().
Parameter source: A string or an AST (Abstract Syntax Trees) object.
Parameter filename: The name of the code file, and some recognizable values are passed if the code is not read from the file.
Parameter model: Specifies the kind of compiled code. can be specified as ' exec ', ' eval ', ' single '.
Parameter flag and Dont_inherit: These two parameters are not currently described, optional parameters.

Version: In python2.3, 2.6, 2.7, 3.2 are different, use to attract attention, compatible with Python3

English Description:

Compile the source into a code or AST object. Code objects can be executed by an EXEC statement or evaluated by a call to Eval (). Source can either is a string or an AST object. The Refer to the AST module, documentation for information, is in the work with AST objects.
The filename argument should give the file from which the code is read; Pass some recognizable value if it wasn ' t read from a file ("is commonly used).
The Mode argument specifies what kind of code must is compiled; It can be ' exec ' if source consists of a sequence of statements, ' eval ' if it consists of a single expression, or ' single ' If it consists of a interactive statement (in the latter case, expression statements this evaluate to something OT Her than None would be printed).
The optional arguments flags and Dont_inherit control which future statements (see PEP 236) affect the compilation of sour Ce. If neither is present (or both be zero) The code is compiled with those future statements, is in effect in the code That's calling compile. If The flags argument is given and Dont_inherit are not (or are zero) then the future statements specified by the flags Argu ment is used in addition to those the would be used anyway. If Dont_inherit is a non-zero and the flags argument is it–the future statements in effect around the call to C Ompile is ignored.
Future statements is specified by BITS which can is bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can is found as the Compiler_flag attribute on the _feature instance in t He FutureModule.
This function raises syntaxerror if the compiled source is invalid, and TypeError if the source contains null bytes.
Note when compiling a string with multi-line code in ' or ' eval ' mode, input must is terminated by at least one new Line character. Facilitate detection of incomplete and complete statements in the code module.
Changed in version 2.3:the flags and Dont_inherit arguments were added.
Changed in version 2.6:support for compiling AST objects.
Changed in version 2.7:allowed use of Windows and Mac newlines. Also input in ' exec ' mode does not has a to end in a newline anymore.

exec (R)
Execute Python code, receive: Code or string

exec (print("test"))

Eval (s)
Executes an expression and gets the result

Eval (7*8)

dir (Class)
Quick view of what features the object provides

Dir (dict)

Help (Class)
Get help

Help (Dict)

divmod (d,d)
Total: 97, showing 10 per page, how many pages

N1 N2 = Divmod (97, 10)

isinstance (Instance,class)
Used to determine whether an object is an instance of a class

" Test " Print (Isinstance (S, str))

Filter (function, iterative object)
Loop The second argument, let each loop element execute the function, and if the function returns a value of True, the element is legal.

Li = [1, 2, 3, 4= filter (None, li)print(list (ret))

Lamda ()
The default return result is the return value

Map (functions, objects that can be iterated (something for loop))
Each element of an iterative object is applied to the function, and the result is returned as a list.

Filter () #函数返回True to add elements to the result
Map () #将函数返回值添加到结果中

Globals ()
All Global Variables
locals ()
All local Variables

Hash (s)
Converted to hash value, commonly used for dictionary key

Len (s)
Returns the length of an object

" character " Print  "utf-8")print(len (b))

max ([list])
Returns the maximum value in a list

Li = [one, one, one= max ([Li])

zip ()
Make each column value of a list a no-group

x = [1, 2, 3= [4, 5, 6= [7, 8, 9= zip (x, y, z)print xyz
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
2.Alex Chicken Soup

Recommendation book: "Linda to see the United States"

Json.loads (s)
Converts a string into a python base data type, and the dictionary must be double quoted key value in the form of a string

3. Decorative Device

Adorners, my understanding is that without affecting the original function (including the execution process, return results, return value, etc.), add other functions (before or after the original function), that is, without modifying the original code under the premise, to achieve the added functionality.

Therefore, the advantages are:

1. The Code modification amount is small, does not affect the original function internal logic and so on;

2. Do not modify the original function code, avoid the original function because of the code grammatical error;

3. Flexible, with the need to add function of the function before adding simple code on the line;

#def outer (func):#print (123,func)#def outer (func):#return "111"defOuter (func):defInner (*args,**kwargs):#support for variable-pass parameters        Print("before") R= Func (*args,**kwargs)#return the original function return value        returnR#return the original function return value        Print(" After")    returnInner#@ + function name#Features:#1. Automatically executes the outer function and passes the function name F1 below as a parameter#2. Re-assign the return value of the outer function to F1@outerdef F1 ():Print("F1")

This article from "YGQYGQ2" blog, declined reprint!

Python Learning record Day4

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.