0

我是 Python 的新手,现在正在玩这些课程,

看看这个简单的代码,

class Testing:

    BB_Key_Length = {256 : 240}

#method __init__() is a special method, which is called class constructor or initialization method that 
#Python calls when you create a new instance of this class.    
    def __init__(self, key_length):
        self._key_length = key_length
        self._n = int(key_length / 8)

        if key_length in self.BB_Key_Length:
            self._b = self.BB_Key_Length[key_length]
            print(self._b)


Object1 = Testing(200)
print(Testing.BB_Key_Length)

在第 13 行,写的是,print(self._b)这也在__init__函数内部,但为什么当我创建对象时 self._b 的值没有打印

Object1 = Testing(200)

我想要的只是打印self._b我无法打印的值

4

1 回答 1

1

为什么当我创建对象时 self._b 的值不打印Object1 = Testing(200)

因为 print 语句在 if 语句中,这是错误的,因为 200 不是 dict 中的键self.BB_Key_Length

于 2013-10-08T18:32:57.977 回答