Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Generators and iterators in Python     - LinSSID: a graphical Wi-Fi scanner under Linux (Linux)

- How to use Monit to deploy a server to monitor the system (Server)

- MySQL uses Federate engine mapping table to achieve operation of the local remote operation table (Database)

- To install the mail client terminal Evolution 3.13.2 under Ubuntu 14.04 (Linux)

- How to create a someone project on github (Linux)

- df show disk space usage (Linux)

- Ubuntu and derivative system users how to install Pinta 1.5 (Linux)

- Oracle view object space usage show_space (Database)

- Elementary OS Freya global menu (Linux)

- Java interface and inheritance (Programming)

- Lua4.0 interpreter entrance (Programming)

- Oracle 10g relations with the constraint of column properties NULLABLE (Database)

- Ubuntu Telnet service settings (Linux)

- How to install Zephyr Test Management Tools on CentOS 7.x (Server)

- Mounting Windows shared directory system under the Linux (Linux)

- Linux file permissions to modify the command: chmod (Linux)

- Expand an existing RAID arrays and remove the failed disk in a RAID (Linux)

- Thinking in Java study notes - Generics (Programming)

- Source Analysis: Java object memory allocation (Programming)

- Using PPA to install the lightweight theme software HotShots 2.1.0 under Ubuntu (Linux)

 
         
  Generators and iterators in Python
     
  Add Date : 2017-08-31      
         
       
         
  Personally feel that iterator and yield to achieve the same function, but iterator need to achieve in the class, yield real function to achieve, both will save the state

The generator is also implemented by iterators

#! / Usr / bin / env python
#coding: utf-8
# Define three functions
Def Lee (name, age):
    Return 'I am% s, I am% d old'% (name, age)
    
Def Marlon ():
    Return 'I am Marlon'
    
Def Allen ():
    Return 'I am Allen'
    
Function_list = [Lee, Marlon, Allen] # There are three functions in the list

# Define a generator
Def MyGenerator (* args):
    For i in args:
        Yield i
        
A = MyGenerator (* function_list) # generator
Execute the next () method, which saves the current execution state, which will be used the next time the next () method is called. Print (a.next, ( 'Lee', 29)
Print apply (a.next ())
Print apply (a.next ())

# Why yield a next method? Look at the following iterator column, will understand why the generator is implemented by the iterator
# The following is an example of an iterator. A class that implements the __iter__ method and the next () method is called an iterator
Class MyIterator (object):
    Def __init __ (self, funcs):
        Self.total_funcs = len (funcs) # Records how many functions to execute in total
        Self.func_list = funcs # Log all functions
        Self.step = 0 # record the current execution to which a function
    Def __iter __ (self):
        Pass
    Def next (self):
        If self.step < self.total_funcs: # When the currently executed function is located less than the total number of functions
            Self.step + = 1 # start
            Return self.func_list [self.step-1] # Execute the current function
        Else:
            Raise StopIteration

C = MyIterator (function_list)
Print apply (c.next (), ( 'Lee', 29))
Print apply (c.next ())
Print apply (c.next ())
     
         
       
         
  More:      
 
- Linux (Ubuntu) How iptables port mapping (Server)
- MongoDB replication instance (Database)
- After Ubuntu Password Forgot your way back (Linux)
- Different between Linux file path and the windows (Linux)
- After CentOS configure SSH password Free, still prompted for a password (Linux)
- Zombie process under Linux (Linux)
- Xtrabackup creates a slave node without downtime (Database)
- Python object-oriented programming (Programming)
- Android Fragment really fully resolve (Programming)
- Analysis examples: Intrusion Response Linux platform Case (Linux)
- Java Builder mode (Programming)
- Shell scripts quickly deploy Tomcat project (Server)
- Ubuntu 12.04 kernel configuration automatically restart and crash dump (Linux)
- Swift used in the application to add a local push at the specified time (Programming)
- Installation and deployment of MariaDB under CentOS (Database)
- Axel install plug under CentOS 5/6 acceleration yum downloads (Linux)
- Linux-du and df command (Linux)
- Ubuntu install Eclipse for C / C ++ and related configuration (Linux)
- JQuery implements the same content merge cells (Programming)
- Linux system components Detailed log (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.