0% found this document useful (0 votes)
2 views57 pages

01 Python Topics

The document covers advanced data science topics in Python, focusing on object-oriented programming (OOP) concepts such as instance, class, and static methods, as well as dunder methods and inheritance. It also explores functional programming principles, including first-class functions, higher-order functions, closures, and decorators. Additionally, it discusses iterators and generators, highlighting their roles in Python programming.

Uploaded by

dongshengche2013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views57 pages

01 Python Topics

The document covers advanced data science topics in Python, focusing on object-oriented programming (OOP) concepts such as instance, class, and static methods, as well as dunder methods and inheritance. It also explores functional programming principles, including first-class functions, higher-order functions, closures, and decorators. Additionally, it discusses iterators and generators, highlighting their roles in Python programming.

Uploaded by

dongshengche2013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CMSC480:

Advanced Data Science


Lecture 1: Selected Python Topics

Dongsheng Che
Computer Science Department
East Stroudsburg University
Object-Oriented
Programming (OOP)

Instance, Class and Static


Methods
Python Classes

Source: [Link]
Python Instance and Class Variables
Using Instance and Class Methods
Instance, Class, and Static Methods
Instance methods need
a class instance and can
access the instance
through self.

Class methods don’t


need a class instance.
They can’t access the
instance (self) but they
have access to the class
itself via cls.
Static methods don’t have access to cls or
self. They work like regular functions but
belong to the class’s namespace.

Source: [Link]
Instance, Class, and Static Methods

Source: [Link]
Instance, Class, and Static Methods
• Class method Used to access or modify the class state. It can modify the
class state by changing the value of a class variable that would apply
across all the class objects.
• The instance method acts on an object’s attributes. It can modify the
object state by changing the value of instance variables.
• Static methods have limited use because they don’t have access to the
attributes of an object (instance variables) and class attributes (class
variables). However, they can be helpful in utility such as conversion
form one type to another.
• Class methods are used as a factory method. Factory methods are those
methods that return a class object for different use cases. For example,
you need to do some pre-processing on the provided data before
creating an object.

Source: [Link]
Object-Oriented
Programming (OOP)

Instance, Class and Static


Methods
Object-Oriented
Programming (OOP)

Dunder Methods
Dunder methods

Methods like .__init__() and .__str__() are called dunder methods


because they begin and end with double underscores.

Source: [Link]
Dunder methods
• Dunder methods allow instances of a class to interact with the
built-in functions and operators of the language.
• Dunder methods are not invoked directly by the programmer

The dunder method __init__ was called implicitly by the language


when you created your instance of a square.
Source: [Link]
Special Dunder methods
• __str__ method – returns the string representation of an
object.
• __repr__ method – returns the string representation of an
object.
• __eq__ method – defines the equality logic for comparing
objects by values.
• __hash__ method – returns the hash value as an integer.
• __bool__ method – determines whether a custom object is
True or False.
__str__ and __repr__
Object-Oriented
Programming (OOP)

Dunder Methods
Object-Oriented
Programming (OOP)

Inheritance and Super()


17
super() Deep Dive
super() can also take two parameters:
• the first is the subclass, and
• the second parameter is an object that is an instance of that subclass.
Object-Oriented
Programming (OOP)

Inheritance and Super()


Object-Oriented
Programming (OOP)

Callable Instances
What is Callable?
• A callable is any object that you can call using a pair of
parentheses and, optionally, a series of arguments.
• Examples of callables:
• Built-in functions and classes
• User-defined functions that you create with the def keyword
• The constructors of your custom classes
• Instance, class, and static methods
• Instances of classes that implement the .call() method
• Closures that you return from your functions
• Generator functions that you define using the yield keyword

Source: [Link]
Callable Instance
Instances of classes that implement the .call() method

Source: [Link]
__init()__ vs __call()__
• The .init() method is the instance initializer.
• The .call() method turns instances into callable objects.

Source: [Link]
Callable Applications
• Write a callable that takes consecutive numeric values from a data
stream and computes their cumulative average. Between calls, the
callable must keep track of previously passed values.

Source: [Link]
Object-Oriented
Programming (OOP)

Callable Instances
Functional
Programming

What is Functional
Programming?
Functional Programming
• Python supports three major programming paradigms:
• Procedural Programming: Code is structured in blocks
(functions, loops, if statements); simple, but hard to
maintain big code bases.
• Object Oriented Programming (OOP): Code is structured
in interacting objects; this encapsulation makes
independent testing easier, the approach scales better to
larger code bases.
• Functional Programming (FP): Functions are used as the
main building block; a declarative rather than imperative
programming style is used.

Source: [Link]
Functional Programming
• In FP, functions are first class objects. They can be:
• stored in variables,
• passed to other functions as parameters, or
• returned from functions.
• Functions that operate on functions are called higher order
functions.
• Higher order functions (filter, map, apply) and recursion are
preferred over structural constructs (if/else branching, loops).
• New functions are created dynamically by combining existing
functions.

Source: [Link]
Functional
Programming

What is Functional
Programming?
Functional
Programming

Python Inner Functions


Python Inner Functions
• A function defined inside another function is known as an inner
function or a nested function.
• Inner functions can access variables and objects from their
enclosing function.

The name is defined in the


local scope of
outer_func(), we call
them as nonlocal names.
They are nonlocal from the
inner_func() point of
view.

Source: [Link]
Functional
Programming

Python Inner Functions


Functional
Programming

Closures
Closures
• Closure factory functions. Closures are dynamically created functions that
are returned by other functions. Their main feature is that they have full
access to the variables and names defined in the local namespace where
the closure was created, even though the enclosing function has returned
and finished executing.

Source: [Link]
Closures
• A closure is a nested function that references one or more
variables from its enclosing scope.
• Criteria to met by Closures are:
• We must have nested function.
• Nested function must refer to the value defined in the
enclosing function.
• Enclosing function must return the nested function.

Source: [Link]
Closures
• The variable greeting is non-local variable

Source: [Link]
Closures
• The variable greeting is non-local variable

To find the memory


address of the cell
object, you can use
the __closure__ property

Source: [Link]
Closure Application
• The variable data is non-local variable
Functional
Programming

Closures
Functional
Programming

Decorators
Decorators
• A decorator function is:
• A function (here is
deco) ) that takes
another function
(original function, here
is f) as an argument
and returns another
function (or closure,
here is g)
• The closure typically accepts any combination of positional and
keyword-only arguments.
• The closure function calls the original function using the arguments
passed to the closure and returns the result of the function.
• The inner function is a closure because it references the fn argument
from its enclosing scope or the decorator function.
Decorators without argument

Source: [Link]
Decorators with argument

Source: [Link]
Functional
Programming

Decorators
Functional
Programming

Iterators and Iterables


What is an Iterator?
An iterator is an object that implements:
• __iter__ method that returns the object itself.
• __next__ method that returns the next item.

Source:
What is an Iterable?
• An iterable is an
object that you can
iterate over.

• An object is
iterable when it
implements the
__iter__ method.

• The __iter__
method returns a
new iterator.

Source:
Is this Object Iterable?
Use Python iter() function to
test if an object is iterable

Source:
Iterable and Iterator
Functional
Programming

Iterators and Iterables


What is a generator?
• A generator function is a function with yield in it.
• A generator expression is like a list comprehension. It uses "()" vs "[]"
• A generator object is returned by both above
• A generator is always an iterator

Source:
Generator Function v.s. Iterator
Generator Expression v.s. List Comprehension
Functional
Programming

Generators

You might also like