Python object

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 which stress on objects i.e. it mainly emphasize on 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

Understanding of Object

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.

Creating an Object

filter_none

edit
close

play_arrow

link
brightness_4
code

class Cars:
  def __init__(self, m, p):
    self.model = m
    self.price = p
  
Audi = Cars("R8", 100000)
  
print(Audi.model)
print(Audi.price)

chevron_right


Output:

R8
100000

Working of the Program:

Audi = Cars():

  • A block of memory is allocated on heap. The size of memory allocated is decided from the attributes and methods available in that class(Cars).
  • After memory block is allocated, the special method __init__() is called internally. Initial data is stored into the variables throug this method.
  • The location of allocated memory address of instance is returned into 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 Code 
Audi = Car("R8"
Audi.setprice(1000000
print(Audi.getprice()) 

chevron_right


Output:



1000000

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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 class 
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) 
  
# Class variables can be
# accessed using class 
# name also 
print("\nAccessing class variable using class name"
print(Car.audi)         

chevron_right


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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.