0 like 0 dislike
How to assign values for the Class attributes at runtime?
in Python Coding by Wooden (1.4k points) | 10 views

1 Answer

0 like 0 dislike
Best answer

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)


by Wooden (1.4k points)

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Oct 7, 2019 in Data Science by Tedsf Diamond (40.2k points) | 15 views
0 like 0 dislike
0 answers
asked Aug 30, 2019 in Web Development by Tedsf Diamond (40.2k points) | 6 views
Welcome to MathsGee Skills Question and Answer Bank, a platform, where you can ask study questions and receive answers from other members of the community. Help is always 100% FREE!
MathsGee Q&A is the knowledge-sharing community where millions of students and experts put their heads together to crack their toughest homework questions.

Enter your email address:

Popular Courses

Python For First Timers
Python For Everyone