Table of contents
1.
Introduction
2.
Dynamic Attributes and properties in Python
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dynamic Attributes and Properties - Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The attributes or methods of a class are the properties of that class. The attributes are non-callable properties and similarly, methods are the callable properties for a function. There are many concepts are introduced in Python in order to improve the concept of utilizing attributes and methods. For example, introducing the concept of access specifiers to make the attributes and properties securely accessible within or among the classes. Similarly, the concept of dynamicity is introduced for attributes and properties so as to dynamically access and use attributes and properties of a particular class. We will go through the concept of dynamic attributes and properties in python in this article.

Also Read, Divmod in Python, Swapcase in Python

Dynamic Attributes and properties in Python

The concept of dynamic attributes is introduced because of a need that static attributes don’t provide. Let’s say we have a class like this:

class Sample:
	#static attribute
	Static_attribute = 1

a = Sample()
a.Static_attribute = 2
#dynamic attribute or instance attribute
a.dynamic_attribute = 2
print(a.Static_attribute)
print(a.dynamic_attribute)
You can also try this code with Online Python Compiler
Run Code

Output:

2
2

We can see that the attribute “dynamic_attribute” is a dynamic attribute because it is created and used after an object or instance is created, i.e., fairly at runtime. These attributes are called dynamic attributes. We can conclude that the attributes that are declared at runtime or after an object or instance is created are called dynamic attributes.
Let’s take another example, 

class Sample:
	pass
x = Sample()
y = Sample()
x.var1 = 2 
x.var2 = 3
y.var1 = 5
print(x.var1)
print(x.var2)
print(y.var1)
print(y.var2)
You can also try this code with Online Python Compiler
Run Code

Output:

2
3
5
Error, because the object var2 isn’t yet defined for y object. 

This var2 is only defined dynamically for the object x. In this way, these dynamic attributes can work. This concept also involves making the objects or instances secured. 
We can add properties to the class dynamically using the self.__dict__[key] = value
In the key, we have to pass the attribute or property name and the value to the value of that property. You can practice by yourself with the help of online python compiler for better understanding.

Want to know about Fibonacci Series in Python and Convert String to List Python check this out.

Frequently Asked Questions

  1. What are dynamic attributes in Python?
    Dynamic Attributes are the attributes that are declared at runtime or after an object or an instance is created. These only belong to that object or instance only. Thus dynamic attributes are introduced to improve attributes' dynamicity and their security.
  2. What is a property in Python?
    A property is a combination of both attributes and methods or functionality of an object. Generally, everything in python is an object. For example, if we have a car class then, the color, no. of wheels, etc., are the properties of the car. 
  3. How should I declare the attributes and properties of a class dynamically?
    Python is a dynamic language. In fact, everything in python is an object. So we can declare all most all objects or methods or any attributes dynamically using the inbuilt methods like __dict__, __setattr__, __getattr__, etc., and many more.
  4. How __dict__ method works?
    __dict__ method of a particular class is able to store all the metadata of the class. This metadata includes class variables, methods, descriptions, etc. 

Conclusion

I hope you enjoyed this article well by understanding the concept of dynamicity in Python and how to use dynamic attributes and properties in Python. You can explore your learning by googling the things. Thank You!
Hey Ninjas! You can check out more unique courses on machine learning concepts through our official website, Coding Ninjas, and checkout Coding Ninjas Studio to learn through articles and other important stuff to your growth.

You can also check this out: Attributes in DBMS
Happy Learning!

Live masterclass