- Object Oriented Programming – Python
- Class and Objects
What is a Class in Python?
A class is a template to create objects. A class is a blueprint to create instances. A class is a design plan to create objects. Foe example the construction plan for a house is an example of a class. Whereas a built house with the help of construction design plan is an example of its object.
In Python, a class will provide a means of bundling data and functionality together. we create a class to create a new type of object. Now we can use this class to create new objects or instances of the defined class.
Features of a Class Instance
There are two main features of a class instance or object:
1. Attributes
2. Methods
What are attributes of an Object?
Each class instance or object may have attributes. These attributes are used to maintain the state of the object. These attributes contains data of the object in different variables.
Instance Variables
What is a Class in Python?
A class is a template to create objects. A class is a blueprint to create instances.
In Python, a class will provide a means of bundling data and functionality together. we create a class to create a new type of object. Now we can use this
class to create new objects or instances of the defined class.
Features of a Class Instance
There are two main features of a class instance or object:
1. Attributes
2. Methods
What are attributes of an Object?
Each class instance or object may have attributes. These attributes are used to maintain the state of the object. These attributes contains data of the object in
different variables.
Example of Instance Variables
For example, consider a class Math1 to perform addition of two numbers num1 and num2. Here num1 and num2 are called instance variables. Instance variables usually have different values for
How we Define Instance Variables?
We use instance variables in methods. For example, we will define instance variables num1 and num2 in a constructor named __init() as given below:
class Math1:
def __init__(self, num1, num2)
self.num1 = num1
self.num2 = num2
What is a Constructor in Python Classes?
A constructor is a special method of a class which is executed automatically when an object is created. A constructor is used to intialize the instance
variables of the object of a class. The name of a constructor is always __init__. The first parameter of a constructor or any other method is always ‘self’. The
‘sel’f is a keyword to refer to the current class insance or object.
Example of Constructor in a Class
for example we will define a constructor for a class called Math1.
class Math1:
def __init__(self, num1, num2)
self.num1 = num1
self.num2 = num2
Here we will use three arguments named self, num1, num2. The first argument is always a special variable called ‘self’. The first argument to every method
in your class is a special variable called self . Note that whenever your class refers to one of its variables or methods, it must precede them by self, for
example:
self.num1 = num1
self.num2 = num2
The other two arguments num1 and num2 will give values to two instance variables of an object. That is they will initialize the state of the object.
What are Methods of an Object?
Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for
maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
What is an Instance Variable?
An instance variable is the varible belonging to an object.Instance variables are declared within methods (functions defined within a class are called methods). Each object can store its own set of data into its instance variables.
For example, consider a class Math1 which uses two number variables num1 and num2. If we create two objects from Math1 class, named obj1 and obj2. Now obj1 will have its own num1, num2, say with values 10 and 20. Similarly the second object obj2 will have its own set of instance variables num1, num2, say with values 500 and 1000.
Example of Instance Variables
For example, consider a class Math1 to perform addition of two numbers num1 and num2. Here num1 and num2 are called instance variables.
How we Define Instance Variables?
We define instance variables in methods. For example, we will define instance variables num1 and num2 in a constructor which is a special method named __init()__ as given below:
class Math1:
def __init__(self, num1, num2)
self.num1 = num1
self.num2 = num2
What is a Constructor in Python Classes?
A constructor is a special method of a class which is executed automatically when an object is created. A constructor is used to intialize the instance variables of the object of a class. The name of a constructor is always __init__. The first parameter of a constructor or any other method is always ‘self’. The ‘sel’f is a special variable to refer to the current class insance or object.
Example of Constructor in a Class
for example we will define a constructor for a class called Math1.
class Math1:
def __init__(self, num1, num2)
self.num1 = num1
self.num2 = num2
Here we will use three arguments named self, num1, num2. The first argument to every method in your class is a special variable called self . Note that whenever your class refers to one of its variables or methods, it must precede them by self, for example:
self.num1 = num1
self.num2 = num2
The other two arguments num1 and num2 will give values to two instance variables of an object. That is they will initialize the state of the object.
What are Methods of an Object?
The Methods are the functions defined within a class. These methods perform some actions on the data of the objects of that class.
Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
What is an Instance Variable?
An instance variable is the varible belonging to an object.