Categories

Dec 19, 2019 in Python

Q: How To Assign Values For The Class Attributes At Runtime?

1 Answer

Dec 19, 2019
We can specify the values for the attributes at runtime. We need to add an init method and pass input to object constructor. See the following example demonstrating this.

>>> class Human:

    def __init__(self, profession):

        self.profession = profession

    def set_profession(self, new_profession):

        self.profession = new_profession

>>> man = Human("Manager")

>>> print(man.profession)

Manager
Click here to read more about Python
Click here to read more about Insurance

Related questions

Madanswer
Dec 19, 2019 in Python
Jul 2 in Python
May 9 in DevOps
...