Python-yield ternary operation Lambda expression

Source: Internet
Author: User

Yield (saves the execution state of the function)

A function with yield is a generator, which, unlike a normal function, generates a generator that looks like a function call, but does not execute any function code until it calls next () (which is automatically called next () in the For loop) to begin execution. Although the execution process still executes according to the process of the function, each execution to a yield statement is interrupted and an iteration value is returned, and the next execution proceeds from the next statement of yield. It looks as if a function was interrupted several times by yield during normal execution, and each break returns the current iteration value through yield.

The benefits of yield are obvious, and the ability to rewrite a function into a generator yields an iterative capability that computes the value of the next next () rather than the instance of the class, not only the code is concise, but the execution process is exceptionally clear.

#!usr/bin/env python#Coding:utf-8defReadLines (): Seek=0" "Start Position" "     whileTrue:with Open ('E:/temp.txt','R') as F:f.seek (seek) data=F.readline ()ifData:seek=F.tell ()" "finish reading the last position as the next first position" "                yieldDataElse:                return  forIteminchreadlines ():PrintItem
Ternary operation Judgment (simplified code)
temp=Noneif 1>3:    Temp='gt'else:    Temp='lt'    restult='gt' if Else ' LT ' Print Restult
Lambda expressions do operations (simplify code)
def foo (x, y    , z): return x*y+zprint foo (1,3,4) Temp=Lambda x,y,z:x*y+z  Print temp (1,3,4)

Python-yield ternary operation Lambda expression

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.