Python字典初始化

时间:2015-08-13 16:39:50

标签: python dictionary

我正在从书中做教程。但我有错误。我的class属性不可见。我的代码:

    class SimpleGradebook():
        def _init_(self, value = {}):
           self._grades = value

        def add_student(self,name):
           self._grades[name] = []


    book = SimpleGradebook()
    book.add_student('test') 

它说:print(book._grades) AttributeError:' SimpleGradebook'对象没有属性' _grades'

2 个答案:

答案 0 :(得分:2)

python -u otherScript.py --oneParameter one --twoParameter two --threeParameter three --fourParameter four python -u otherScript.py --oneParameter one --twoParameter two --threeParameter three --fourParameter four python -u otherScript.py --oneParameter one --twoParameter two --threeParameter three python -u otherScript.py --oneParameter one --twoParameter two python -u otherScript.py --oneParameter one 上需要两个下划线:

__init__

答案 1 :(得分:1)

初始化的每一侧只有1 -。它应该是这样的:

class SimpleGradebook():
    def __init__(self, value = {}):
       self._grades = value

    def add_student(self,name):
       self._grades[name] = []


book = SimpleGradebook()
book.add_student('test')