Categories
Python Answers

How to programmatically set an attribute with Python?

Spread the love

Sometimes, we want to programmatically set an attribute with Python.

In this article, we’ll look at how to programmatically set an attribute with Python.

How to programmatically set an attribute with Python?

To programmatically set an attribute with Python, we call setattr.

For instance, we write

setattr(x, attr, 'magic')

to call the setattr function to set the x‘s attr attribute to 'magic' where attr is a string with the attribute name.

Conclusion

To programmatically set an attribute with Python, we call setattr.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *