类功能不接受自我。变量

时间:2015-04-14 20:50:02

标签: python class

我正在尝试在类中创建一个用于在Enigma机器中旋转转子的函数。但是,当我试图告诉它使用哪个转子时它不接受它,如下面的代码所示:

from collections import deque

class Rotors:
    def __init__(self):
        self.a = deque("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        self.vi = deque("JPGVOUMFYQBENHZRDKASXLICTW")
        self.vii = deque("NZJHGRCXMYSWBOUFAIVLPEKQDT")           
        self.viii = deque("FKQHTLXOCBJSPDZRAMEWNIUYGV") 
        self.rotor_vi = [self.a, self.vi]
        self.rotor_vii = [self.a, self.vii]
        self.rotor_viii = [self.a, self.viii]
        self.rotors = [self.rotor_vi, self.rotor_vii, self.rotor_viii]
        self.reflector = deque("FVPJIAOYEDRZXWGCTKUQSBNMHL")


    def rotate_rotor(self, rotor):
        rotor.rotate(1)

x = Rotors()
x.rotate_rotor(self.vi)

其输出为:

 Traceback (most recent call last):
  File "C:\Users\Aaron\Documents\Programs\Enigma.py", line 20, in <module>
    x.rotate_rotor(self.vi)
NameError: name 'self' is not defined
[Finished in 0.1s]

1 个答案:

答案 0 :(得分:4)

x.rotate_rotor(self.vi)

应该是:

x.rotate_rotor(x.vi)