MODULE 5 Chapter 15: Classes and objects Chapter 16: Classes and functions Chapter 17: Classes and methods
What is a Class and Objects in Python? Class : The class is a user-defined data structure that binds the data members and methods into a single unit. Class is a blueprint or code template for object creation . Using a class, you can create as many objects as you want. Object : An object is an instance of a class . It is a collection of attributes (variables) and methods. We use the object of a class to perform actions. Objects have two characteristics: They have states and behaviors (object has attributes and methods attached to it) Attributes represent its state, and methods represent its behavior. Using its methods, we can modify its state. An object is a real-life entity. It is the collection of various data and functions that operate on those data.
Every object has the following property. Identity : Every object must be uniquely identified. State : An object has an attribute that represents a state of an object, and it also reflects the property of an object. Behavior : An object has methods that represent its behavior.
What are classes and objects in Python? Python is an object oriented programming language. Class is a abstract data type which can be defined as a template or blueprint that describes the behavior / state that the object of its type support. An object is an instance of class . Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating Object is simply a collection of data (variables) and methods (functions) that act on those data . The process of creating this object is called instantiation
Defining a Class in Python We define a class using the keyword class. The first string is called docstring and has a brief description about the class . Although not mandatory, this is recommended. A class attributes are data members( variables) and functions .
S y n t a x The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows − class ClassName: “”” Optional class documentation string' class_suite””” class_suite The class has a documentation string, which can be accessed via ClassName. doc . The class_suite consists of all the component statements defining class members, data attributes and functions.
Example The header indicates that the new class is called Point . The body is a docstring that explains what the class is for. You can define variables and methods inside a class definition .
Creating an Object in Python Class can be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call. Example : blank = Point() The return value is a reference to a Point object, which we assign to blank.
Exampl e 1 1 e1
Example 2
The variable empCount is a class variable whose value is shared among all instances of a this class. This can be accessed as Employee.empCount from inside the class or outside the class. Th e fi r s t m e thod init () is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. We can declare other class methods like normal functions with the exception that the first argument to each method is self . Python adds the self argument to the list for you ; you do not need to include it when you call the methods.
Creating Instance Objects To create instances of a class, you call the class using class name and pass in whatever arguments its init method accepts emp1 = Employee(“Raju", 2000) emp2 = Employee(“Swamy", 5000)
Accessing Attributes We can access the object's attributes using the dot operator with object. Class variable would be accessed using class name as follows − emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount)
Example2 : Rectangle Rectangle can be created using following two possibilities: You could specify one corner of the rectangle (or the center), the width, and the height . You could specify two opposing corners .
Instances as return values Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:
Objects are mutable You can change the state of an object by making an assignment to one of its attributes. For example, to change the size of a rectangle without changing its position, you can modify the values of width and height: box.width = box.width + 50 box.height = box.height + 100
Modifying through functions You can also write functions that modify objects. For example, grow_rectangle takes a Rectangle object and two numbers, dwidth and dheight , and adds the numbers to the width and height of the rectangle: def grow_rectangle(rect, dwidth, dheight): rect.width += dwidth rect.height += dheight
Copying Copying an object is often an alternative to aliasing. The copy module contains a function called copy that can duplicate any object:
Exercise: 1. Write a definition for a class named Circle with attributes center and radius, where center is a Point object and radius is a number. 2. Write a function called draw_rect that takes a Turtle object and a Rectangle and uses the Turtle to draw the Rectangle