ITERATORS GENERATORS DECORATORS Cheat Sheet

A Programming › Python cheat sheet in English by sahusourabh

This explains Iterators, Generators and decorators in one page

7,389 views 1,274 downloads 22 Jul 2022 History

ITERATORS

Looping

Important functions to be implem­ented

__iter__()

__next__()

__iter__() : takes iteratable object like list, tuples

__next­__(): is used to return the next value in iteration

Use of iterators

Use of iterators

Iterators with class

Iterators with class

GENERATORS

1. Generator generate one element at a time from a sequence.

2. Yield is used to get the value

3 It saves the state not like function where once function is called state will be returned to new call

USE

USE

List Compre­hension vs Generation

Compre­hen­sion: all in one go

Generation : one by one ...fast

EXAMPLE

EXAMPLE

DECORATORS

1. A decorator is a special function which adds some extra functi­onality to an existing function

2. A decorator is a function that accepts a function as a parameter and returns a function.

3. Decorators are useful to perform some additional processing required by a function.

Want to add addition functi­onality to function

Want to add addition functionality to function

@decor

showve­hicle = decor(­sho­wve­hicle) instead of this line, you can use @decor

@decor implem­ent­ation

@decor implementation

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.