Show Menu
Cheatography

ITERATORS GENERATORS DECORATORS Cheat Sheet by

This explains Iterators, Generators and decorators in one page

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

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

List Compre­hension vs Generation

Compre­hen­sion: all in one go

Generation : one by one ...fast

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

@decor

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

@decor implem­ent­ation

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by sahusourabh

          PYTHON REGULAR EXPRESSION CHEATSHEET Cheat Sheet
          FUNCTIONS IN PYTHON Cheat Sheet