Mar-30-2019, 09:16 PM
I agree with @DeaD_EyE that it can make to have user input call outside of class
Here a way with class that is flexible as it can do both ways.
Here a way with class that is flexible as it can do both ways.
class Person: def __init__(self, name='', age=''): self.name = name self.age = age @property def name_age(self): return f'My name is {self.name}. I am {self.age}.' @staticmethod def user_input(): return Person( input('Please enter your name: '), input('Please enter your age: '))Use:
>>> # Normal way >>> p = Person('Kent', 30) >>> p.age 30 >>> p.name_age 'My name is Kent. I am 30.' >>> # Do it with user input >>> p1 = Person.user_input() Please enter your name: Tom Please enter your age: 35 >>> p1.name 'Tom' >>> p1.name_age 'My name is Tom. I am 35.' >>> p.name_age 'My name is Kent. I am 30.'