我是 python 的绝对初学者。我想知道我是否可以在 Class() 中使用额外的键值对?
class Users():
def __init__(self, first_name, last_name, **others):
for key, value in others.items():
self.key = value
self.first = first_name
self.last = last_name
def describe_user(self):
print("The user's first name is " + self.first)
print("The user's last name is " + self.last)
for key, value in others.items():
print("The user's " + key + " is " + value)
user1 = Users('Joseph', 'Wilson', age = '18', location = 'California')
print(user1.location)
user1.describe_user()
错误:
AttributeError: 'Users' object has no attribute 'location'
NameError: name 'others' is not defined
守候你守候我
相关分类