Python object - GeeksforGeeks

11 Jan, 2022
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 object-oriented programming language that stresses on objects i.e. it mainly emphasizes functions. 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 
For a better understanding of the concept of objects 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.
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 the copy of each data variables 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. 
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
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 into 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.
Accessing Class Member Using Object:
Variable and method of a class are accessible by using class objects or instances.
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
# Python program to create instance# variables inside methods     class Car:             # Class Variable    vehicle = 'car'                # The init method or constructor    def __init__(self, model):                     # Instance Variable        self.model = model                     # Adds an instance variable    def setprice(self, price):        self.price = price             # Retrieves instance variable        def getprice(self):            return self.price         # Driver CodeAudi = Car("R8")Audi.setprice(1000000)print(Audi.getprice())
Output: 
1000000
Example 2:
Python3
class Car:         # Class Variable    vehicle = 'Car'                    # The init method or constructor    def __init__(self, model, price):             # Instance Variable            self.model = model        self.price = price             # Objects of classAudi = 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) # Class variables can be# accessed using class# name alsoprint("\nAccessing class variable using class name")print(Car.vehicle)   # orprint(Audi.vehicle)   # orprint(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. 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 is 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 Python class
Deleting an Object:
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 
ruhelaa48
mq56
Python-OOP
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Defaultdict in Python
How to Install PIP on Windows ?
Deque in Python
Bar Plot in Matplotlib
Check if element exists in list in Python
Python math function | sqrt()
Python | Output Formatting
Python - Pandas dataframe.append()
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects