I'm practicing OOP concepts in Python, and I came across this problem:
class User:
def __init__(self, username, email, password,
firstname, lastname, phone):
self.username = ""
self.email = ""
self.password = ""
self.firstname = ""
self.lastname = ""
self.phone = ""
user=User('x','y','z','f','v','c')
print(vars(user))
Result:
{'username': '', 'firstname': '', 'lastname': '', 'phone': '', 'password': '', 'email': ''}
The values are not saved to the object. How can I fix this?
You need to assign the function arguments to the instance variables.
class User:
def __init__(self, username, email, password,
firstname, lastname, phone):
self.username = username
self.email = email
self.password = password
self.firstname = firstname
self.lastname = lastname
self.phone = phone
user=User('x','y','z','f','v','c')
print(vars(user))
Save the constructor arguments to the appropriate instance variables:
def __init__(self, username, email, password, firstname, lastname, phone):
self.username = username
# etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.