Python类的getter/setter

Python类的getter/setter,python,dictionary,encapsulation,setter,getter,Python,Dictionary,Encapsulation,Setter,Getter,因此,我正在建立一个Python类,用于获取/设置字典的各个部分。我正在尝试设置它,以便有一个字典(例如ID number:Names)和一个变量,该变量是字典中当前选定的成员 我正在尝试实现它,以便让getter和setter返回当前选择的字典值,以及带有try/except块的setter将当前选择更改为字典中的另一个成员。我不关心传递ID号,我只需要确保我可以通过getter检索名称。这是我的代码: class Classroom: def __init__(self):

因此,我正在建立一个Python类,用于获取/设置字典的各个部分。我正在尝试设置它,以便有一个字典(例如ID number:Names)和一个变量,该变量是字典中当前选定的成员

我正在尝试实现它,以便让getter和setter返回当前选择的字典值,以及带有try/except块的setter将当前选择更改为字典中的另一个成员。我不关心传递ID号,我只需要确保我可以通过getter检索名称。这是我的代码:

class Classroom:

    def __init__(self):

        self.__classList = {
            001: Mark,
            002: Kevin,
            003: Stacy}
        self.currentStudent = self.__classList[001] #start at the beginning of the list

    @property
    def currentStudent(self):
        return self.__currentStudent

    @currentStudent.setter
    def currentStudent(self, ID):
        try:
            currentStudent = __classList[ID]
        except:
            print("Invalid ID entered")
当我去测试代码时,根据我对使用@property的理解,如果我输入以下内容:

classroom = Classroom()
classroom.currentStudent = 002
print(classroom.currentStudent)

我应该有“凯文”的丝网印不?我现在得到的是屏幕正在打印002。我做错了什么?

我对它做了一点修改,使它工作起来。我想你用的是python2.7

class ClassRoom(object):

    def __init__(self):

        self.__classList = {
            001: "Mark",
            002: "Kevin",
            003: "Stacy"}
        self.__currentStudent = self.__classList[001] #start at the beginning of the list

    @property
    def currentStudent(self):
        return self.__currentStudent

    @currentStudent.setter
    def currentStudent(self, ID):
        try:
            self.__currentStudent = self.__classList[ID]
        except:
            print("Invalid ID entered")

class_room_1 = ClassRoom()
class_room_1.currentStudent = 002
试验


我对它做了一点修改,使它可以工作。我想你用的是python2.7

class ClassRoom(object):

    def __init__(self):

        self.__classList = {
            001: "Mark",
            002: "Kevin",
            003: "Stacy"}
        self.__currentStudent = self.__classList[001] #start at the beginning of the list

    @property
    def currentStudent(self):
        return self.__currentStudent

    @currentStudent.setter
    def currentStudent(self, ID):
        try:
            self.__currentStudent = self.__classList[ID]
        except:
            print("Invalid ID entered")

class_room_1 = ClassRoom()
class_room_1.currentStudent = 002
试验


您没有创建
教室
的实例。您正在处理类对象本身,它的行为非常不同。而且,您的setter无论如何都会被破坏。
currentStudent=\uu classList[ID]
为什么对
\uu classList
使用双下划线?无论如何,您需要回到OOP的基础。您将实例与类混淆。在setter中,您只是将
\uu classList
字典中的一些值赋给一个局部变量
currentStudent
,该变量在方法返回调用方时被丢弃,实际上什么也不做。您认为您在哪里创建了
教室
的实例?您只有一行,
classification.currentStudent=002
正在处理该类,实际上删除了您的属性并将其替换为
002
(顺便说一句,在Python3和Python2中,它不再是有效的语法,只是简单地等同于
2
,因此您应该只使用一个字符串).您没有创建
教室
的实例。您正在处理类对象本身,它的行为非常不同。而且,您的setter无论如何都会被破坏。
currentStudent=\uu classList[ID]
为什么对
\uu classList
使用双下划线?无论如何,您需要回到OOP的基础。您将实例与类混淆。在setter中,您只是将
\uu classList
字典中的一些值赋给一个局部变量
currentStudent
,该变量在方法返回调用方时被丢弃,实际上什么也不做。您认为您在哪里创建了
教室
的实例?您只有一行,
classification.currentStudent=002
正在处理该类,实际上删除了您的属性并将其替换为
002
(顺便说一句,在Python3和Python2中,它不再是有效的语法,只是简单地等同于
2
,因此您应该只使用一个字符串)当前学生be
False
似乎令人困惑。当前学生==class\u-1。当前学生be
False
似乎令人困惑。