Skip to content

Chapter 6: Closures & GeneratorsΒ #8

@chrisoemma

Description

@chrisoemma

Closures:

According to google a closure is an act or process of closing something, especially an institution, thoroughfare, or frontier, or of being closed

In python, A closure is an inner function that remembers and has access to variables of the local scope which was created, even after the outer function has finished executing.

hint: A closure closes over free variables from their environment.

Discussion Points:

  • Use of regular expression in a closure

  • A list of functions

  • A list of patterns

  • A file of patterns

Generators:

There is a lot of work in building an iterator in Python. We have to implement a class with iter() and next() method, keep track of internal states and raise StopIteration when there are no values to be returned.

This is both lengthy and counterintuitive. This is when generators dive in

Python generators are way of creating iterators

Discussion points:

  • A Fibonacci Generator

  • A Plural Rule Generator

Hint: We use generators over other loops for mainly two reasons:

  • more performance

  • Easy to read and write

I know you argue with list compressions that are also easy to write right?...YES but the good of generators is you can use them with list comprehension and get performance as a bonus too

Metadata

Metadata

Assignees

Labels

class notesNew class notes or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions