Introduction To Programming 2
Python
Lecture 9
Department of Computer Engineering
Astana IT University
Zamart Ramazanova
Senior-Lecturer
[Link]@[Link]
February 2025
Topics
• Decorators
• Iterator
• Generator
• Regular Expressions
2
Decorators
• Decorators are a very powerful and useful tool in
Python.
• Decorator allows programmers to modify the behavior of
a function or class.
• A Decorator is used to wrap a function.
• It gives new functionality without changing the original
function.
3
Copyright © 2018 Pearson Education, Inc.
What’s a Decorator
Decorators themselves take the form of callable objects
(e.g., functions) that process other callable objects
• Function decorators install wrapper objects to intercept
later function calls and process them as needed.
• Class decorators install wrapper objects to intercept
later instance creation calls and process them as
required.
4
Copyright © 2018 Pearson Education, Inc.
First Class Objects
In Python, functions are first class objects which
means that functions in Python can be used or passed
as arguments.
Properties of first-class functions:
• A function is an instance of the Object type.
• You can store the function in a variable.
• You can pass the function as a parameter to another
function.
• You can return the function from a function.
• You can store them in data structures such as hash
tables, lists, …
5
Copyright © 2018 Pearson Education, Inc.
Example
Ex. Passing the function as an argument
6
Copyright © 2018 Pearson Education, Inc.
How to define a decorator
function?
• It takes a function (the one being decorated), wrap it
in a wrapper function and then return the wrapper
function.
• There are user defined and in-built decorators.
staticmethod and classmethod are example of in-
built decorators.
• There can be more that one decorator for one
function. They are executed using stack.
7
Copyright © 2018 Pearson Education, Inc.
Passing argument to the
decorated function
The wrapper function should accept the arguments which
are being passed to the function being decorated.
• You can also define wrapper for class method.
8
Copyright © 2018 Pearson Education, Inc.
Class Decorators
• Class decorators are similar to function decorators.
• They are run at the end of a class statement to rebind a
class name to a callable.
• They can be used to manage class when they are
created.
• They can be used to insert a layer of wrapper logic to
manage instances.
• Remember both class and class instance are object in
Python
9
Copyright © 2018 Pearson Education, Inc.
Example – Singleton Class
10
Copyright © 2018 Pearson Education, Inc.
Iterators
• An iterator in Python is an object that is used to iterate
over iterable objects like lists, tuples, dicts, and sets.
• The Python iterators object is initialized using
the iter() and the next() method for iteration.
• __iter__(): The iter() method is called for the initialization
of an iterator. This returns an iterator object
• __next__(): The next method returns the next value for
the iterable. If there is no more items to return then it
should raise StopIteration exception.
11
Copyright © 2018 Pearson Education, Inc.
Python iter() Example
12
Copyright © 2018 Pearson Education, Inc.
Python next() Example
An iterator’s next method produces items
13
Copyright © 2018 Pearson Education, Inc.
Infinite Iterators
• It is not necessary that the item in an
iterator object has to exhaust.
• There can be infinite iterators (which never
ends)
• We must be careful when handling such
iterator.
14
Copyright © 2018 Pearson Education, Inc.
Examples
15
Copyright © 2018 Pearson Education, Inc.
Iterable vs Iterator
• Python iterable and iterator are
different.
• The main difference between them is,
iterable in Python cannot save the state
of the iteration, whereas in iterators the
state of the current iteration gets
saved.
Note: Every iterator is also an iterable, but
not every iterable is an iterator in Python.
16
Copyright © 2018 Pearson Education, Inc.
Iterating on an Iterable
Iterating on each item of the iterable.
17
Copyright © 2018 Pearson Education, Inc.
Iterating on an iterator
Iterating on each item of the iterable.
18
Copyright © 2018 Pearson Education, Inc.
Getting StopIteration Error while
using iterator
Iterable in Python can be iterated over multiple times, but
iterators raise StopIteration Error when all items are
already iterated.
19
Copyright © 2018 Pearson Education, Inc.
Generators
• Generators simplifies creation of iterators
• A generator is a function that produces a sequence of
results instead of a single value.
• A generator is a special routine that can be used to
control the iteration behavior of a loop.
• A generator is similar to a function returning an array.
• A generator has parameters, it can be called and it
generates a sequence of result.
• But unlike functions, which return a whole array, a
generator yields one value at a time
20
Copyright © 2018 Pearson Education, Inc.
Generators
Generators in Python:
• Are defined with the def keyword
• Use the yield keyword
• May use several yields keywords
• Return an iterator
21
Copyright © 2018 Pearson Education, Inc.
Example to Generator
22
Copyright © 2018 Pearson Education, Inc.
Example to Generator
• A generators is also an iterator.
• The word “generator” is confusingly used to mean both
the function that generates and what it generates.
• When a generator function is called, it returns a
generator object without even beginning execution of the
function.
• When next method is called for the fist time, the function
starts executing until it reaches yield statement.
• The yielded value is returned by the next call.
23
Copyright © 2018 Pearson Education, Inc.
Example to Generator
24
Copyright © 2018 Pearson Education, Inc.
Generator Expressions
• There are two types of generators in Python:
generator functions and generator expressions.
• A generator function is any function in which the keyword
yield appears in its body.
• The appearance of the keyword yield is enough to make
the function a generator function.
• The other type of generators are the generator
equivalent of a list comprehension. Its syntax is really
elegant for a limited use case.
25
Copyright © 2018 Pearson Education, Inc.
Relationship between
Generator and Iterator
26
Copyright © 2018 Pearson Education, Inc.
Advantages
• Cleaner code
• Iterators can work with infinite sequences
• Iterators save resources
• Generator allows to write streaming code with fewer
intermediate variables and data structures.
• Memory and CPU efficient
27
Copyright © 2018 Pearson Education, Inc.
Regular Expressions
• A regular expression is a special sequence of characters
that helps you match or find other strings or sets of
strings, using a specialized syntax held in a pattern.
• Regular expressions are widely used in UNIX world.
• RE (also called as regex) is a string that contains
special symbols and characters to find and extract the
information
• Module: re contains the methods like
compile()
search()
match()
findall()
28
split()Copyright
… © 2018 Pearson Education, Inc.
Regular Expressions
• Regular expressions (regexp) are a text-matching tool
embedded in Python.
• They are useful in creating string searches and string
modifications
• You can always use regular Python instead, but regexp
are often much easier
• Documentation: [Link]
29
Copyright © 2018 Pearson Education, Inc.
Regular Expressions Steps
Import re
• Step 1: Compile the RE
• Step 2: Search the strings
• Step 3: Display the result
30
Copyright © 2018 Pearson Education, Inc.
Regular Expressions Example
Example 1: search()
31
Copyright © 2018 Pearson Education, Inc.
Regular Expressions Example
Example 2: findall()
32
Copyright © 2018 Pearson Education, Inc.
Regular Expressions Example
Example 3: match()
33
Copyright © 2018 Pearson Education, Inc.
Sequence characters
• Match only one character in the string
34
Copyright © 2018 Pearson Education, Inc.
Summary
• This chapter covered:
• Decorators
- Function and Class decorators
• Iterators
- the iter() and the next() method for iteration
• Generators
• Regular Expressions
- Regular expressions methods
35
Ramazanova Zamart. Introduction to Programming 2