0% found this document useful (0 votes)
24 views4 pages

Python Classes and Object-Oriented Concepts

The document defines key programming concepts such as classes, objects, and instance variables in Python, explaining their roles and relationships. It discusses class definition, instantiation, and the use of special methods like __init__() and __str__() for object initialization and representation. Additionally, it covers pure functions, modifiers, the prototype and patch development methodology, copying objects using the copy module, and the printing of objects with examples.

Uploaded by

WOLFIE
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)
24 views4 pages

Python Classes and Object-Oriented Concepts

The document defines key programming concepts such as classes, objects, and instance variables in Python, explaining their roles and relationships. It discusses class definition, instantiation, and the use of special methods like __init__() and __str__() for object initialization and representation. Additionally, it covers pure functions, modifiers, the prototype and patch development methodology, copying objects using the copy module, and the printing of objects with examples.

Uploaded by

WOLFIE
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

Module-5 1.

Define the terms with example: (i) Class (ii) Objects (iii) Instance
variables
Classes and objects, Classes and functions & Classes and methods (i) Class: Class is a user-defined data type which binds data and functions
together into single entity. Class is just a prototype (or a logical entity/blue
print) which will not consume any memory. A class can have a set of
1. Define the terms with example: (i) class (ii) objects (iii) instance variables (or attributes, member variables) and member functions
variables (methods).

2. Explain the following with syntax and suitable code snippet: A class in Python can be created using a keyword class. Here, we are
i) Class definition ii) instantiation iii) passing an instance (or objects) creating an empty class without any members by just using the keyword
as an argument iv) instances as return values. pass within it.
class Point:
3. Discuss __str__() and __init__() methods used in class definition pass
print(Point)
4. Define pure function and modifier. Explain the role of pure functions The output would be –
and modifiers in application development with suitable python <class '__main__.Point'>
programs
(ii) Objects: An object is an instance of a class and it has physical existence.
5. Explain the program development concept ‘prototype and patch’ with One can create any number of objects for a class. A class can have a set of
suitable example. variables (or attributes, member variables) and member functions
(methods).
6. Explain the concept of copying using copy module in with an example. p.x =10.0
p.y =20.0
7. Briefly explain the printing of objects with examples. A state diagram that shows an object and its attributes is called as object
diagram. For the object p, the object diagram is shown in Figure.
(iii) Instance variables: At the class level, variables are referred to as class (iii) Passing an instance (or objects): An object is an instance of a class and
variables, whereas variables at the instance level are called instance it has physical existence. One can create any number of objects for a class.
variables. A class can have a set of variables (or attributes, member variables) and
member functions (methods).
p.x =10.0
p.y =20.0
A state diagram that shows an object and its attributes is called as object
diagram. For the object p, the object diagram is shown in Figure.
2. Explain the following with syntax and suitable code snippet:
i) Class definition ii) instantiation iii) passing an instance (or objects)
as an argument iv) instances as return values.

(i) Class definition: Class is a user-defined data type which binds data and
functions together into single entity. Class is just a prototype (or a logical
entity/blue print) which will not consume any memory. A class can have a
set of variables (or attributes, member variables) and member functions
(methods).
A class in Python can be created using a keyword class. Here, we are
creating an empty class without any members by just using the keyword
pass within it. 3. Discuss __str__() and __init__() methods used in class definition.
class Point:
pass __str__(): __str__ is a special method, like __init__, that is supposed to return
print(Point) a string representation of an object.
The output would be – For example, here is a str method for Time objects:
<class '__main__.Point'> # inside class Time:
def __str__(self):
(ii) Instantiation: The process of creating a new object is called as return '%.2d:%.2d:%.2d' % ([Link], [Link], [Link])
instantiation and the object is instance of a class. When you print an object, Python invokes the str method:
print(p) >>> time = Time(9, 45)
The output would be – >>> print(time)
<__main__.Point object at 0x003C1BF0> 09:45:00
The output displays the address (in hexadecimal format) of the object in I write a new class, I almost always start by writing __init__, which makes
the memory. It is now clear that, the object occupies the physical space, it easier to instantiate objects, and __str__, which is useful for debugging.
whereas the class does not. As an exercise, write a str method for the Point class. Create a Point object
and print it.
__init__(): (A method __init__() has to be written with two underscores The function creates a new Time object, initializes its attributes, and returns a
before and after the word init) Python provides a special method called as reference to the new object. This is called a pure function because it does not
__init__() which is similar to constructor method in other programming modify any of the objects passed to it as arguments and it has no effect, like
languages like C++/Java. The term init indicates initialization. As the name displaying a value or getting user input, other than returning a value.
suggests, this method is invoked automatically when the object of a class is
created. Consider the example given here Modifiers: Sometimes it is useful for a function to modify the objects it gets
as parameters. In that case, the changes are visible to the caller. Functions that
import math work this way are called modifiers.
class Point: Assume that, we need to add few seconds to a time object, and get a new time.
def __init__(self,a,b): Then, we can write a function as below
self.x=a def increment(t, seconds):
self.y=b [Link] += seconds
def dist(self,p2): while [Link] >= 60:
d=[Link]((self.x-p2.x)**2 + (self.y-p2.y)**2) [Link] -= 60
return d [Link] += 1
def __str__(self): while [Link] >= 60:
return "(%d,%d)"%(self.x, self.y) [Link] -= 60
p1=Point(10,20) #__init__() is called automatically [Link] += 1
p2=Point(4,5) #__init__() is called automatically
The above function is a modifier.
print("P1 is:",p1) #__str__() is called automatically
print("P2 is:",p2) #__str__() is called automatically 5. Explain the program development concept ‘prototype and patch’ with
d=[Link](p2) #explicit call for dist() suitable example.
print("The distance is:",d Whenever we do not know the complete problem statement, we may write the
program initially, and then keep of modifying it as and when requirement
4. Define pure function and modifier. Explain the role of pure functions and (problem definition) changes. This methodology is known as prototype and
modifiers in application development with suitable python programs. patch. That is, first design the prototype based on the information available
Pure Functions: A pure function is a function whose output value follows and then perform patch-work as and when extra information is gathered. But,
solely from its input values, without any observable side effects. this type of incremental development may end-up in unnecessary code, with
Here is a simple prototype of add_time: many special cases and it may be unreliable too.
def add_time(t1, t2): Here is a function that converts Times to integers:
sum = Time() def time_to_int(time):
[Link] = [Link] + [Link] minutes = [Link] * 60 + [Link]
[Link] = [Link] + [Link] seconds = minutes * 60 + [Link]
[Link] = [Link] + [Link] return seconds
return sum
6. Explain the concept of copying using copy module in with an example. >>> [Link] = 45
Aliasing can make a program difficult to read because changes in one place >>> [Link] = 00
might have unexpected effects in another place. It is hard to keep track of all >>> print_time(start)
the variables that might refer to a given object. 09:45:00
Copying an object is often an alternative to aliasing. The copy module To make print_time a method, all we have to do is move the function
contains a function called copy that can duplicate any object: definition inside the class definition. Notice the change in indentation.
>>> p1 = Point()
>>> p1.x = 3.0
>>> p1.y = 4.0
>>> import copy
>>> p2 = [Link](p1)
p1 and p2 contain the same data, but they are not the same Point.
>>> print_point(p1)
(3, 4)
>>> print_point(p2)
(3, 4)
>>> p1 is p2
False

7. Briefly explain the printing of objects with examples.


In Python, this can be achieved by using __repr__ or __str__ methods.
__repr__ is used if we need a detailed information for debugging while
__str__ is used to print a string version for the users.
class Time:
"""Represents the time of day."""
def print_time(time):
print('%.2d:%.2d:%.2d' % ([Link], [Link], [Link]))
To call this function, you have to pass a Time object as an argument:
>>> start = Time()
>>> [Link] = 9

You might also like