Classes in Python. General concepts. The class keyword. Class objects. Instance objects
Contents
- 1. Features of using classes in Python. Class objects and instance objects
- 2. Creating a class object. General form
- 3. Creating an instance object. General form
- 4. Features of class objects
- 5. Features of instance objects
- 6. What is a class attribute? The example of class Circle
- 7. An example of declaring and using a simple class that stores some value
- 8. An example implementation of a class that stores a set of data. Class SquareEquation – solving the quadratic equation
- Related topics
Search other websites:
1. Features of using classes in Python. Class objects and instance objects
In the Python programming language, classes are characterized by the following features:
- individually, classes represent specific namespaces;
- classes support the creation of a set of objects;
- class namespaces can be inherited;
- in classes, you can implement overloading of functions (methods);
- in classes, you can implement overloading of operators.
As you know, in Python, any element (number, string, list, etc.) is represented by an object. This also applies to the Python object-oriented model, which distinguishes between two types of objects:
- class objects. These objects implement the default behavior. On the basis of classes of objects are objects instances. Class objects are factories for creating instance objects;
- instance objects. These are objects that are created on the basis of class objects. Instance objects have their own (independent) namespace and also inherit the names (have access) of the classes from which they were created.
⇑
2. Creating a class object. General form
A class object is created using a statement that contains the class keyword. In the simplest case, the general form of creating a class object is:
class ClassName: # Elements (attributes) implemented in the class # ...
here
- ClassName – the name of the class to create. In this class, you can implement various elements of the class, such as internal data, functions (methods), operators, etc.
On the basis of the name ClassName can be created real object instance.
⇑
3. Creating an instance object. General form
An instance object is created by calling the class name on the right side of the assignment operator =. Each time a class name is called, a new instance of an object of that class is created.
The general form of creating an instance object is as follows:
Obj = ClassName()
here
- Obj – the name of the instance object that is created by the class object named ClassName. The created instance object Obj has its own namespace;
- ClassName – the name of the class object that is created using the class instruction.
⇑
4. Features of class objects
In the Python language for class objects, the following features can be distinguished:
- a class object is created using a class statement, which is defined as an executable statement (you can call it from program code). Calling a class statement creates a new class object;
- inside the class statement, you can create attributes of the class object. This is done using the assignment operator =. Class attributes are accessed by their composite (full) names, for example Obj.NameAtr (an attribute named NameAtr in a class object named Obj);
- class attributes describe the state of the object. The state of an object means the current value of the data within the object;
- class attributes describe the behavior of an object. Object behavior refers to the presence of methods that handle instances. Methods in classes are defined using the def statement.
⇑
5. Features of instance objects
An instance object is the result of calling a class object. For instance objects, the following characteristic features can be distinguished:
- an instance object is created by calling the class object as a function that contains parentheses ( ). Any number of instance objects can be created from one class object;
- an instance object is a specific data item in the program;
- when created, each instance object inherits the attributes of the class from which it is created;
- the instance object has its own namespace;
- the assignment of values to attributes occurs through the first argument, which can be, for example, a reference named self (see examples below). Once assigned, the corresponding attribute is created on each individual instance object. In this case, the class methods receive as their first argument a self reference (by convention) that corresponds to the instance object. Using this reference (self), you can create or modify the data of the object instance. Data of class instance cannot be created (changed) using self.
⇑
6. What is a class attribute? The example of class Circle
A class attribute is an element that is implemented in the class. This element can be a method, operator, data field, etc.
For example. Let a class named Circle be given, which implements a circle on the coordinate plane.
# Classes. Class attributes. Example. # Declare a Circle class that implements a circle on a coordinate plane class Circle: # Method that sets the value of the circle center (x, y) and radius def Set(cr, x, y, radius): cr.x = x cr.y = y cr.radius = radius return # Get data about the circle as a list def Get(cr): return [cr.x, cr.y, cr.radius] # return as list def Print(cr): print('x = ', cr.x, ', y = ', cr.y, ', radius = ', cr.radius) return # Calculate the area of circle def Area(cr): return 3.1415*cr.radius*cr.radius # Declare an instance of the class object circle1 = Circle() # Set the value of coordinates circle1.Set(2, 3, 5) # Display data about circle1 circle1.Print() # Calculate the area of a circle area = circle1.Area() print('circle1.Area() = ', area)
The above Circle class implements the following attributes:
- method Set(), which sets the value of the x, y, radius attributes;
- method Get(), which returns data about the circle as a list;
- method Print() – displays information about the circle;
- method Area() – displays the area of a circle of radius radius;
- internal data fields x, y – coordinates of the circle center;
- data field radius – the radius of a circle.
In methods, the first argument is a parameter named cr, which denotes the current instance. This cr name is used to access data: cr.x, cr.y, cr.radius. The cr name can be replaced with another name such as self.
⇑
7. An example of declaring and using a simple class that stores some value
The example declares a class named Value. In the class, a data cell is created that contains some value. The name of the data in the class is value. To access the data, the self parameter is used, which represents the current instance of the class. It is allowed to use another name instead of self (for example, self2).
# Classes. Declaration of the class. Using of class # Declare a Value class that contains some value class Value: # Method declaration in Value class def SetValue(self, val): self.value = val # self.value - the value to be stored # Method that returns value def GetValue(self): return self.value def Print(self): print("value = ", self.value) # Case 1. Calling methods through the instance object obj1 = Value() # Declare an object of class Value obj1.SetValue(25) # Set new value in the class object obj1.Print() # value = 25 # Case 2. Calling the SetValue() method through a class object obj2 = Value() # Declare an object of class Value Value.SetValue(obj2, 37) Value.Print(obj2) # value = 37
The result of the program
value = 25 value = 37
Figure 1 shows the objects that are created in the above example.
Figure 1. Program structure: instance objects and class object
⇑
8. An example implementation of a class that stores a set of data. Class SquareEquation – solving the quadratic equation
The example instantiates a class that is designed to solve a quadratic equation. In the class, using the instance name self, three data members named a, b, c are created. Also, in the methods of the class, additional internal variables with the names d, x1, x2 are used. These are only temporary local variables. They are not instance data and do not affect the state of a particular object instance. The internal state of any instance of the object is determined by the data a, b, c.
# Classes. Example. import math # Declare a SquareEquation class that solves a quadratic equation class SquareEquation: # Class data initialization method def __init__(self, a, b, c): # three data members of the class are created self.a = a self.b = b self.c = c return # Method for calculating the roots of an equation def Calc(self): # d - discriminant, additional local variable d = self.b*self.b - 4*self.a*self.c if (d<0): print("The equation has no roots") return [] else: # x1, x2 - local variables are not data members of the class x1 = (-self.b - math.sqrt(d))/(2.0*self.a) x2 = (-self.b + math.sqrt(d))/(2.0*self.a) return [x1, x2] # Using the class SquareEquation equation1 = SquareEquation(1, 1, -2) # a specific instance of an object result = equation1.Calc() print('result = ', result)
The result of the program
result = [-2.0, 1.0]
⇑
Related topics
⇑