+ 3

Hello SoloLearn community, How do i store user input in a class in Python ?

How do i write a code that ask user the details i want and this details are stored in the class abd be used later.

22nd Mar 2019, 6:24 AM
Sunday Afolabi
Sunday Afolabi - avatar
3 Antworten
+ 12
class Person(): def __init__(self): self._name = input() def get_name(self): return self._name p = Person() print(p.get_name()) That's just one of several possible ways
22nd Mar 2019, 6:57 AM
Anna
Anna - avatar
+ 7
Nope, __init__(self) is called for every instance you create of the class. "self" always refers to an instance of the class. If you create 10 instances of Person(), you'll be asked for input ten times and each input will be assigned to the instance variable self._name of each respective instance
22nd Mar 2019, 8:02 AM
Anna
Anna - avatar
+ 1
But this will output same input for all object i create since it is done using the __init__ method, how do i make it different ? Anna
22nd Mar 2019, 7:47 AM
Sunday Afolabi
Sunday Afolabi - avatar