
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Different Ways to Access Instance Variable in Python
The Instance variables are generally used to represent the state or attributes of an object. Each instance of a class can have its own set of instance variables, which can store unique values. An instance variable is defined within the methods of a class and is accessible throughout the instance's lifespan.
Accessing the instance variable in python
The flexibility that comes with usage of instance variables allows each instance to maintain its own set of variables, enabling customized behaviour and data storage for different objects. Thus, accessing instance variables in python is a useful step.There are different ways to access the instance variable in python, let's see them one by one.
Using the dot notation
The Instance variables can be accessed using the dot notation along with the instance name. This is the easy and simple way to access the instance variable. Following is the syntax for using the dot notation.
instance.variable_name
Where,
instance is the instance.
variable_name is the name of the variable.
represents the dot notation.
Example
In this example, we will access the instance variable from the user defined class by using the dot notation then the instance variable is returned as the output.
class sample_class: def __init__(self, variable): self.variable = variable instance = sample_class("Welcome to Tutorialspoint, Have a happy learning!") print(instance.variable)
Output
Following is the output of accessing the instance variable using the dot notation.
Welcome to Tutorialspoint, Have a happy learning!
Using the self keyword
In a class the self keyword is the reference of the instance. The Instance variables in the class can be accessed by using the self.variable_name. Following is the syntax for using the self keyword to access the instance variable.
self.variable_name
Example
If we want to access the instance variable from the defined class, we can use the self keyword.
class sample: def __init__(self, variable): self.variable = variable def print_variable(self): print(self.variable) instance = sample("Welcome to Tutorialspoint, Have a happy learning!") instance.print_variable()
Output
Welcome to Tutorialspoint, Have a happy learning!
Using the dict attribute
In every instance in Python language, a dictionary named __dict__ is present, it contains all the instance variables. So, we can access an instance variable with the help of __dict__ attribute. The following is the syntax for using the dict attribute.
instance.__dict__[variable_name]
Example
Here in this example, we will use the __dict__ attribute to get the instance variable of the class.
class sample: def __init__(self, variable): self.variable = variable instance = sample("Python is one of the popular programming languages") print(instance.__dict__['variable'])
Output
Python is one of the popular programming languages
Using the getattr() function
Python provides a builtin function namely getattr() that takes two arguments; an object and a string. This function returns the value of the specified attribute within the object. Following is the syntax for using the getattr() function.
getattr(instance, 'variable_name')
Example
If we want to get the instance variable from the user defined class we need to pass the instance and the variable name to the getattr() function.
class sampe_class: def __init__(self, variable): self.variable = variable instance = sampe_class("Hello, Python is one of the popular programming languages!") print(getattr(instance, 'variable'))
Output
Hello, Python is one of the popular programming languages!
Using the hasattr() function
The hasattr() function is as same as the getattr() function but the only difference is the hasattr() returns the Boolean values True or False. If the variable is present in the class, then output will be returned as True otherwise returned as False.
Syntax
The following is the syntax for using the getattr() function.
hasattr(instance, 'variable_name')
Example
Following example demonstrates the hasattr() function to get the instance variable.
class sampe_class: def __init__(self, variable): self.variable = variable instance = sampe_class("Hello, Python is one of the popular programming languages!") print(hasattr(instance, 'variable'))
Output
True