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:
Example 1: Using Self and object reference
#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) |
chevron_right
filter_none
Output:
hello my name is: HARRY my roll number is: 1001 HARRY
Example 2: Using getattr()
# 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') |
chevron_right
filter_none
Output:
Harsh True 152
Recommended Posts:
- Python VLC Instance - Creating MediaListPlayer Instance
- Python VLC Instance - Creating MediaList Instance
- Python VLC Instance - Creating new Media Instance
- Python VLC Instance - Creating MediaPlayer Instance
- Python | Ways to split a string in different ways
- Class and Instance Attributes in Python
- Instance method in Python
- Launching AWS EC2 Instance using Python
- Python VLC MediaPlayer - Getting Instance of it
- Python VLC Instance – Deleting Single Media
- Python VLC Instance - Enumerate the defined audio output devices
- Python VLC Instance – Enabling Media/Broadcast
- Python VLC Instance – Setting Loop on the media
- Python VLC Instance – Play the specific Media/Broadcast
- Python VLC Instance – Stop the specific Broadcast
- Python VLC Instance - Creating Media path
- Python VLC Instance - Setting Application Name
- Python VLC Instance - Setting Meta Data of Application
- Python VLC Instance - Unsetting Logging Callbacks
- Python VLC - Instance
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.

