Python object
Last Updated :
21 Jun, 2023
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. Python is an object-oriented programming language that stresses objects i.e. it mainly emphasizes functions. Python Objects are basically an encapsulation of data variables and methods acting on that data into a single entity.
Note: For more information, Python Classes and Objects
Understanding of Python Object
For a better understanding of the concept of objects in Python. Let’s consider an example, many of you have played CLASH OF CLANS, So let’s assume base layout as the class which contains all the buildings, defenses, resources, etc. Based on these descriptions we make a village, here the village is the object in Python.
Syntax:
obj = MyClass()
print(obj.x)
Instance defining represent memory allocation necessary for storing the actual data of variables. Each time when you create an object of class a copy of each data variable defined in that class is created. In simple language, we can state that each object of a class has its own copy of data members defined in that class.
Creating a Python Object
Working of the Program: Audi = Cars()
- A block of memory is allocated on the heap. The size of memory allocated is decided by the attributes and methods available in that class(Cars).
- After the memory block is allocated, the special method __init__() is called internally. Initial data is stored in the variables through this method.
- The location of the allocated memory address of the instance is returned to the object(Cars).
- The memory location is passed to self.
Python3
class Cars:
def __init__( self , m, p):
self .model = m
self .price = p
Audi = Cars( "R8" , 100000 )
print (Audi.model)
print (Audi.price)
|
Output:
R8
100000
Accessing Class Member Using Object:
Variables and methods of a class are accessible by using class objects or instances in Python.
Syntax:
obj_name.var_name
Audi.model
obj_name.method_name()
Audi.ShowModel();
obj_name.method_name(parameter_list)
Audi.ShowModel(100);
Example 1:
Python3
class Car:
vehicle = 'car'
def __init__( self , model):
self .model = model
def setprice( self , price):
self .price = price
def getprice( self ):
return self .price
Audi = Car( "R8" )
Audi.setprice( 1000000 )
print (Audi.getprice())
|
Output:
1000000
Example 2:
Python3
class Car:
vehicle = 'Car'
def __init__( self , model, price):
self .model = model
self .price = price
Audi = Car( "R8" , 100000 )
BMW = Car( "I8" , 10000000 )
print ( 'Audi details:' )
print ( 'Audi is a' , Audi.vehicle)
print ( 'Model: ' , Audi.model)
print ( 'price: ' , Audi.price)
print ( '\n BMW details:' )
print ( 'BMW is a' , BMW.vehicle)
print ( 'Model: ' , BMW.model)
print ( 'Color: ' , BMW.price)
print ( "\nAccessing class variable using class name" )
print (Car.vehicle)
print (Audi.vehicle)
print (BMW.vehicle)
|
Output:
Audi details:
Audi is a Car
Model: R8
price: 100000
BMW details:
BMW is a Car
Model: I8
Color: 10000000
Accessing class variable using class name
Car
Car
Car
Self Variable:
SELF is a default variable that contains the memory address of the current object in Python. Instance variables and methods can be referred to by the self-variable. When the object of a class is created, the memory location of the object is contained by its object name. This memory location is passed to the SELF internally, as SELF knows the memory address of the object, so the variable and method of an object are accessible. The first argument to any object method is SELF because the first argument is always object reference. This process takes place automatically whether you call it or not.
Example:
Python3
class Test:
def __init__(Myobject, a, b):
Myobject.country = a
Myobject.capital = b
def myfunc(abc):
print ( "Capital of " + abc.country + " is:" + abc.capital)
x = Test( "India" , "Delhi" )
x.myfunc()
|
Output:
Capital of India is: Delhi
Note: For more information, refer to self in the Python class
Deleting an Object in Python:
Python Object property can be deleted by using the del keyword:
Syntax:
del obj_name.property
objects also can be deleted by del keyword:
Syntax:
del obj_name
Please Login to comment...