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

1 Answer

0 votes
by

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)

Manager

Top 500 Python Questions and answers.

Related questions

0 votes
asked Oct 12, 2021 in Python by rajeshsharma
0 votes
asked Feb 11, 2021 in Python by SakshiSharma
...