in Python by
How to assign values for the Class attributes at runtime?

1 Answer

0 votes

We can  specify the values for the python class 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)


Top 500 Python Questions and answers.

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

Related questions

0 votes
asked Jul 24, 2020 in Ionic by Robindeniel
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked May 27, 2019 in Gradle by Ankita1283
0 votes
asked Aug 29, 2020 in Python by Robindeniel
0 votes
asked Apr 8, 2020 in SAP by amita rallin