Python Full Stack development 14

Source: Internet
Author: User
Tags eval iterable

Advantages of iterators:
1. Save Memory
2. Inertia mechanism
3. Unidirectional, irreversible
How to tell if it is an iterative object and an iterator

  Iterators that iterate over an object

Iterative Object __iter__ (): It becomes an iterator
1. ' __iter__ ' in Dir (obj): True or Flase, returns true to represent that he is an iterative object
    2. Introduction of Modules
From collections Import Iterator (iterator)
Print (isinstance (parameter name, Iterator)
From collections import iterable (Iteration object)
Print (isinstance (parameter name, Itera) ble
  Generator:
An iterator that writes its own Python code is the generator.
The generator itself is an iterator
Generator expressions and lists push-down
1. List-derived
[Variable (processed variable) for variable in iterable (Iteration object)] # loop mode
[Variable (processed variable) for variable in iterable if condition]# filter mode
2. Generator expression: Just like the list expression, the parentheses are different.

(Variable (processed variable) for variable in iterable (Iteration object)) # Loop mode
(Variable (processed variable) for variable in iterable if condition) # Filter Mode

Pros: 1. Save code
Cons: Bad troubleshooting
Overall: Generally used in a list deduction structure, in other ways can also, very complex list, list derivation is not constructed

Built-in functions

1.1 Scope-related
A =33b = 22def func ():    a = 1    print (Globals (), ' \ n ', locals ()) func ()
1.21 Other related
Execution of String type code Eval,exec,complie
Print (eval (' 1*2*3*4*5*6*7*8*9 ')) Li = "" For I in Range:    print (i) "" "Exec (LI)
1.22 input and output related print and input
k = Print (1,2,4,6,sep= ' ****** ', end= ') with open (' Test. txt ', ' w ', encoding= ' utf-8 ') as F:    print (' 111111111111 ', 1111 , file=f,sep= ' * * * * ') print ('    111111111111 ', 1111,file=f,sep= ')    print (' 111111111111 ', 1111,file=f,sep= ‘****‘)
1.23 memory-related hash (): Gets the hash value of a hash object, ID (return memory address)
Print (hash (1020000000000000000)) # number up to 19 to guarantee that the hash value is the same as the value, and it becomes another print (hash (' 1111111111111111111 ')) # hash string print ( Hash ((1,2,3,4)) # Hashion Group print (hash (True)) # hash bool Value
The Open: function opens a file, creates a Files object, and the related method can call it to read and write 1.25 help (): Helps a = 1help (a) #查看帮助
1.26 Call related callable () can call return true cannot call return False
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

Python Full Stack development 14

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.