【发布时间】:2018-12-13 14:14:43
【问题描述】:
我有这种平静的代码,我试图在一个类中打印出一个字典,我如何从不同的方法访问值并指定要打印的值
class Student:
student_instance_dict = {}
def __init__(self, student):
self.name = None
self.phoneNumber = 0
students = ['Mike', 'Dany']
for student in students:
instance = Student(student)
Student.student_instance_dict[student] = instance
print (Student.student_instance_dict[student].name)#this prints out without any problems
def printUpdates(x):
print (Student.student_instance_dict[student].x)
name = 'name'
printUpdates(name)
las 行返回 AttributeError: 'Student' object has no attribute 'x'
【问题讨论】:
标签: python class dictionary