0% found this document useful (0 votes)
52 views19 pages

Notes Module5 Python Programming

This document summarizes key concepts from Chapters 15-17 of the Think Python textbook on classes and objects. Chapter 15 discusses defining classes, attributes, embedding objects as attributes, copying objects, and checking types with isinstance(). Chapter 16 covers pure functions, modifiers that mutate objects, and prototyping vs planning. Chapter 17 explains object-oriented features like methods, printing objects, operator overloading, type-based dispatch, and polymorphism through interfaces and implementations.

Uploaded by

varshitham
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)
52 views19 pages

Notes Module5 Python Programming

This document summarizes key concepts from Chapters 15-17 of the Think Python textbook on classes and objects. Chapter 15 discusses defining classes, attributes, embedding objects as attributes, copying objects, and checking types with isinstance(). Chapter 16 covers pure functions, modifiers that mutate objects, and prototyping vs planning. Chapter 17 explains object-oriented features like methods, printing objects, operator overloading, type-based dispatch, and polymorphism through interfaces and implementations.

Uploaded by

varshitham
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
You are on page 1/ 19

MODULE 5

TextBook : Think Python

Module 5 Syllabus:

Classes and objects: Programmer-defined types, Attributes, Rectangles, Instances as


return values, Objects are mutable, Copying,

Classes and functions: Time, Pure functions, Modifiers, Prototyping versus planning,

Classes and methods: Object-oriented features, Printing objects, Another example, A


more complicated example,The init method, The __str__ method, Operator overloading,
Type-based dispatch, Polymorphism, Interface and implementation,

1
Chapter 15
Classes and objects

15.1 Programmer-defined types

A programmer-defined type is also called a class. A class definition looks like this:

class Point:
"""Represents a point in 2-D space."""

15.2 Attributes

2
Object Diagram

>>> blank.x=4.0
>>> blank.y=5.0

3
15.3 Embedded objects

4
5
15.6 Copying

>>> p1==p2
False

The is operator indicates that p1 and p2 are not the same object.
the default behavior of the == operator is the same as the is operator; it checks object identity, not
object equivalence.

6
isinstance()

Syntax:
isinstance(objectname,classname)

● isinstance() returns True if the specified object name belongs to the class else it returns False.
.

● isinstance() returned True - it indicates p is an instance(object) of class Point.

7
hasattr()

Syntax:
hasattr(objectname,’attributename’)

8
Chapter 16
Classes and functions

16.1 Pure Functions

9
16.2 Modifiers

10
11
12
Chapter 17
Classes and methods

Methods and Functions

13
14
15
17.5 __init__() method

16
17.6 __str__() method

17.7 Operator overloading

● By defining other special methods, you can specify the behavior of operators on
programmer-defined types. For example, if you define a method named __add__ for the Time
class, you can use the + operator on Time objects.
● Changing the behavior of an operator so that it works with programmer-defined types is called
operator overloading.

17
17.8 Type-based dispatch

Here are examples that use the + operator with different types.

__radd__() method

18
17.9 Polymorphism

19

You might also like