Python Learning Record day4, python record day4

Source: Internet
Author: User

Python Learning Record day4, python record day4
1. built-in function supplement

Callable (object)
Check whether the object is callable
1. classes can be called.
2. The instance cannot be called unless the _ call _ method is declared in the class.

def f1():    print("test")f2 = "test"print(callable(f1))print(callable(f2))
True
False

Chr (I)
Returns the ASCII character corresponding to integer I.

print(chr(81))
Print (chr (81 ))

Odr (c)
The parameter c is an ascii character, and the return value is a decimal integer.

print (ord('b'))
Print (ord ('B '))

Random verification code

import randomli = []for i in range(5):    temp = random.randrange(65,91)    c = chr(temp)    li.append(c)result = "".join(li)print(result)
 

Compile (source, filename, mode [, flags [, dont_inherit])
Compile source as a code or AST object. Code objects can be executed using exec statements...
Compile (source, filename, mode [, flags [, dont_inherit])
Chinese description: Compile source as a code or AST object. Code objects can be evaluated using exec statements or eval.
Source: string or AST (Abstract Syntax Trees) object.
Filename: the name of the code file. If the code is not read from the file, some identifiable values are passed.
Parameter model: specifies the type of the compiled code. You can specify it as 'exec ', 'eval', and 'singles '.
Flags and dont_inherit are optional.

Versions: different versions are available in python2.3, 2.6, 2.7, and 3.2. Be careful when using Python 3.

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 be a string or an AST object. Refer to the ast module documentation for information on how to work with AST objects.
The filename argument should give the file from which the code was 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 be 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 single interactive statement (in the latter case, expression statements that evaluate to something other than None will be printed).
The optional arguments flags and dont_inherit control which future statements (see PEP 236) affect the compilation of source. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile. If the flags argument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can be found as the compiler_flag attribute on the _Feature instance in the future module.
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 'single' or 'eval' mode, input must be terminated by at least one newline character. This is to 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 have to end in a newline anymore.

Exec (r)
Execute python code and receive: code or string

exec(print("test"))

Eval (s)
Execute the expression and obtain the result

eval(7*8)

Dir (class)
Quick view of what functions the object provides

dir(dict)

Help (class)
Get help

help(dict)

Divmod (d, d)
Total: 97, 10 entries per page, how many pages are required

n1 n2 = divmod(97, 10)

Isinstance (instance, class)
Used to determine whether the object is an instance of a class

s = "test"print(isinstance(s, str))

Filter (function, iteratable object)
The second parameter of the loop allows each loop element to execute the function. If the return value of the function is True, the element is valid.

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

Lamda ()
The returned result is returned by default.

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

Filter () # The function returns True and adds the element to the result.
Map () # Add the function return value to the result

Globals ()
All global variables
Locals ()
All local variables

Hash (s)
Converts data to hash values, which are generally used for dictionary keys.

Len (s)
Returns the length of an object.

S = "character" print (len (s) B = bytes (s, encoding = "UTF-8") print (len (B ))

Max ([list])
Returns the maximum value in the list.

li = [11, 22, 33]r = max([li])

Zip ()
Groups the values of each column in the list into a non-group

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

Recommendation: Linda to the United States

Json. loads (s)
Converts a string to the basic data type of python. the string-form dictionary must be a double quotation mark to reference the key value.

3. decorator

Decorator. I understand that other functions (before or after the original function) are added without affecting the original function (including the execution process, returned results, and returned values ), that is, the added function is implemented without modifying the original code.

Therefore, its advantages include:

1. A small amount of code modification does not affect the internal logic of the original function;

2. Do not modify the original function code to avoid syntactic errors of the original function;

3. Strong flexibility. Just add simple code before functions that need to be added;

# Def outer (func): # print (123, func) # def outer (func): # return "111" def outer (func): def inner (* args, ** kwargs): # supports variable parameter print ("before") r = func (* args, ** kwargs) # return the original function return value return r # return the original function return value print ("after") return inner # @ + function name # function: #1. automatically execute the outer function and pass the function f1 as a parameter #2. returns the value of the outer function to f1 @ outerdef f1 (): print ("F1 ")

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.