Different ways to access Instance Variable in Python Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 Comment More infoAdvertise with us Next Article Different ways to access Instance Variable in Python mastersiddharthgupta Follow Improve Article Tags : Python python-oop-concepts Practice Tags : python Similar Reads Assign Function to a Variable in Python In Python, functions are first-class objects, meaning they can be assigned to variables, passed as arguments and returned from other functions. Assigning a function to a variable enables function calls using the variable name, enhancing reusability.Example:Python# defining a function def a(): print( 3 min read Get Variable Name As String In Python In Python, getting the name of a variable as a string is not as straightforward as it may seem, as Python itself does not provide a built-in function for this purpose. However, several clever techniques and workarounds can be used to achieve this. In this article, we will explore some simple methods 3 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-va 3 min read Convert String into Variable Name in Python There may be situations where you want to convert a string into a variable name dynamically. In this article, we'll explore how to convert a string into a variable name in Python with four simple examples. Convert String into Variable Name in PythonWhile Python does not directly allow you to convert 3 min read How To Print A Variable's Name In Python In Python, printing a variable's name can be a useful debugging technique or a way to enhance code readability. While the language itself does not provide a built-in method to directly obtain a variable's name, there are several creative ways to achieve this. In this article, we'll explore five simp 3 min read How to use/access a Global Variable in a function - Python In Python, variables declared outside of functions are global variables, and they can be accessed inside a function by simply referring to the variable by its name.Pythona = "Great" def fun(): # Accessing the global variable 'a' print("Python is " + a) fun()OutputPython is Great Explanation:Here a i 3 min read How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like 4 min read Class or Static Variables in Python All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by a class CSStudent. The class may have a static variable whose value is "cse" for all objects. 9 min read Difference between dir() and vars() in Python As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir( 2 min read How to import variables from another file in Python? To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your cur 4 min read Like