The Wayback Machine - https://web.archive.org/web/20241127031453/https://www.geeksforgeeks.org/different-ways-to-access-instance-variable-in-python/
Open In App

Different ways to access Instance Variable in Python

Last Updated : 27 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Instance attributes are those attributes that are not shared by objects. Every object has its own copy of the instance attribute i.e. for every object, instance attribute is different. There are two ways to access the instance variable of class:

  • Within the class by using self and object reference.
  • Using getattr() method

Example 1: Using Self and object reference 

Python3




#creating class
class student:
      
    # constructor
    def __init__(self, name, rollno):
          
        # instance variable
        self.name = name
        self.rollno = rollno
       
    def display(self):
          
        # using self to access 
        # variable inside class
        print("hello my name is:", self.name)
        print("my roll number is:", self.rollno)
          
# Driver Code
# object created
s = student('HARRY', 1001)
  
# function call through object
s.display()
  
# accessing variable from 
# outside the class
print(s.name)


Output:

hello my name is: HARRY
my roll number is: 1001
HARRY

Time complexity: O(1) for both creating the class and calling the display() method and accessing the name variable outside the class.

Auxiliary space: O(1) as there are no additional data structures used and the memory space is constant.

Example 2: Using getattr() 

Python3




# Python code for accessing attributes of class 
class emp: 
    name='Harsh'
    salary='25000'
    def show(self): 
        print(self.name)
        print(self.salary)
          
# Driver Code
e1 = emp() 
# Use getattr instead of e1.name 
print(getattr(e1,'name'))
    
# returns true if object has attribute 
print(hasattr(e1,'name'))
    
# sets an  attribute  
setattr(e1,'height',152) 
    
# returns the value of attribute name height 
print(getattr(e1,'height'))
    
# delete the attribute 
delattr(emp,'salary') 


Output:

Harsh
True
152


Similar Reads

Python VLC Instance - Creating MediaListPlayer Instance
In this article we will see how we can create a MediaListPlayer instance from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we
2 min read
Python VLC Instance - Creating MediaList Instance
In this article we will see how we can create a MediaList Instance from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can c
2 min read
Python VLC Instance - Creating new Media Instance
In this article we will see how we can create a Media instance from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can creat
2 min read
Python VLC Instance - Creating MediaPlayer Instance
In this article we will see how we can create a MediaPlayer instance from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can
2 min read
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-value pairs. In this article, we will see how to use
3 min read
Program to access different columns of a multidimensional Numpy array
Prerequisite: Numpy module The following article discusses how we can access different columns of multidimensional Numpy array. Here, we are using Slicing method to obtain the required functionality. Example 1: (Accessing the First and Last column of Numpy array) C/C++ Code # Importing Numpy module import numpy as np # Creating a 3x3 Numpy array ar
3 min read
How to access different rows of a multidimensional NumPy array?
Let us see how to access different rows of a multidimensional array in NumPy. Sometimes we need to access different rows of multidimensional NumPy array-like first row, the last two rows, and even the middle two rows, etc. In NumPy , it is very easy to access any rows of a multidimensional array. All we need to do is Slicing the array according to
3 min read
Instance method in Python
A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. 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
2 min read
Launching AWS EC2 Instance using Python
In this article, we will learn how python can be used for creating and managing Amazon Web Services (AWS) such as Elastic Compute Cloud (EC2), Simple Storage Service (S3), Relational Database Service (RDS). For this purpose, we will use boto3 library of python, Boto is a Python package that provides interfaces to Amazon Web Services (AWS). Prerequi
4 min read
Python VLC MediaPlayer - Getting Instance of it
In this article we will see how we can get the instance of the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediaPlayer object is the basic object in vlc module for playing the video. MediaPlayer cla
2 min read
Python VLC Instance – Deleting Single Media
In this article we will see how we can delete the single media from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can creat
2 min read
Python VLC Instance - Enumerate the defined audio output devices
In this article we will see how we can get the enumerate audio output devices from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance obje
3 min read
Python VLC Instance – Enabling Media/Broadcast
In this article we will see how we can enable or disable the single media from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object w
2 min read
Python VLC Instance – Setting Loop on the media
In this article we will see how we can set loop on the single media from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can
2 min read
Python VLC Instance – Play the specific Media/Broadcast
In this article we will see how we can play specific media from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can create me
2 min read
Python VLC Instance – Stop the specific Broadcast
In this article we will see how we can specific media/broadcast from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can crea
2 min read
Python VLC Instance - Creating Media path
In this article we will see how we can create a media path from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can create me
2 min read
Python VLC Instance - Setting Application Name
In this article we will see how we can set the application name from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can crea
2 min read
Python VLC Instance - Setting Meta Data of Application
In this article we will see how we can set the meta information of the application from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance
2 min read
Python VLC Instance - Unsetting Logging Callbacks
In this article we will see how we can unset the logging callbacks from the Instance class in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can c
2 min read
Python VLC - Instance
In this article we will see how we can create a Instance class instance in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. Instance act as a main object of the VLC library with the Instance object we can create media player,
2 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Python Attributes: Class Vs. Instance Explained
In Python, attributes are properties associated with objects. They can be variables or methods that are defined within a class or an instance of a class. Understanding the difference between class and instance attributes is fundamental in object-oriented programming. Here's an explanation: Python Attributes: Class Vs. Instance ExplainedBelow, is th
4 min read
Creating Instance Objects in Python
In Python, an instance object is an instantiation of a class, and it is a unique occurrence of that class. Creating instance objects is a fundamental concept in object-oriented programming (OOP) and allows developers to work with and manipulate specific instances of a class. This article will explore the process of creating instance objects in Pyth
3 min read
Class and Instance Attributes in Python
Class attributes: Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility. [GFGTABS] Python # Write Python code here class sampleclass: count = 0 # class attribute def increase(self): sampleclass.count += 1 # Calling increase() on an
2 min read
How to apply different titles for each different subplots using Plotly in Python?
Prerequisites: Python Plotly In this article, we will explore how to apply different titles for each different subplot. One of the most deceptively-powerful features of data visualization is the ability for a viewer to quickly analyze a sufficient amount of information about data when pointing the cursor over the point label appears. It provides us
2 min read
Extending a list in Python (5 different ways)
In this article, we are going to learn different methods of extending a list in Python. The list is the widely used Python data structure and it can be extended by adding elements to the list. There are various methods to extend the list in Python which includes using an inbuilt function such as append(), chain() and extend() function and using the
3 min read
Different ways to Invert the Binary bits in Python
We know how binary value for numbers look like. For example, the binary value for 10 (Number Ten) is 1010 (binary value). Sometimes it is required to inverse the bits i.e., 0's to 1's ( zeros to ones) and 1's to 0's (ones to zeros). Here are there few ways by which we can inverse the bits in Python. 1) Using Loops: By iterating each and every bit w
3 min read
Different ways to convert a Python dictionary to a NumPy array
In this article, we will see Different ways to convert a python dictionary into a Numpy array using NumPy library. It’s sometimes required to convert a dictionary in Python into a NumPy array and Python provides an efficient method to perform this operation. Converting a dictionary to NumPy array results in an array holding the key-value pairs of t
3 min read
Different Ways of Using Inline if in Python
Python offers a concise and expressive way to handle conditional logic in your code by using inline if. Whether you need an essential conditional expression or want to nest multiple conditions, inline can make your code more readable and maintainable. Among these tools is the inline if statement, an invaluable asset for crafting short, yet intuitiv
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg