python类继承,attributeError

Python class inheritance, attributeError

我用python编写了一个StateMachine类,这样它就可以从中继承。它的逻辑按预期工作,但我可以从名为StateMachineTest的派生StateMachine类中存在的State访问属性self.data

生成以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Traceback (most recent call last):
File"/home/nbout/work/python/state_machine/main.py", line 68, in
   main()
File"/home/nbout/work/python/state_machine/main.py", line 59, in main
   test = StateMachineTest()
File"/home/nbout/work/python/state_machine/main.py", line 47, in __init__
   StateMachine.__init__(self, Started())
File"/home/nbout/work/python/state_machine/state_machine.py", line 17, in _init__
   self.current_state.on_enter(self)
File"/home/nbout/work/python/state_machine/main.py", line 16, in on_enter
   print("Started: data:{}".format(sm_test.data))
Started: on_enter
AttributeError: 'StateMachineTest' object has no attribute 'data'
Started: on_exit

状态机.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class State:

    def on_enter(self, state_machine):
        pass

    def on_exit(self, state_machine):
        pass


class StateMachine:

    def __init__(self, start_state):
        self.current_state = start_state

        self.current_state.on_enter(self)

    def __del__(self):
        self.current_state.on_exit(self)

    def set_state(self, state):

        self.current_state.on_exit(self)
        self.current_state = state
        self.current_state.on_enter(self)

Me.Py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from state_machine import StateMachine
from state_machine import State


class StateTest(State):
    def pause(self, state_machine_test):
        pass

    def start(self, state_machine_test):
        pass


class Started(StateTest):
    def on_enter(self, sm_test):
        print("Started: on_enter")
        print("Started: data:{}".format(sm_test.data))

    def on_exit(self, sm_test):
        print("Started: on_exit")

    def pause(self, sm_test):
        print("Started: pause")
        sm_test.set_state(Paused())

    def start(self, sm_test):
        print("Started: start")


class Paused(StateTest):
    def on_enter(self, sm_test):
        print("Paused: on_enter")

    def on_exit(self, sm_test):
        print("Paused: on_exit")

    def pause(self, sm_test):
        print("Paused: pause")

    def start(self, sm_test):
        print("Paused: start")
        sm_test.set_state(Started())


class StateMachineTest(StateMachine):

    def __init__(self):
        StateMachine.__init__(self, Started())
        self.data = 10

    def pause(self):
        self.current_state.pause(self)

    def start(self):
        self.current_state.start(self)


def main():

    test = StateMachineTest()

    test.start()
    test.pause()
    test.pause()
    test.start()


if __name__ == '__main__':
    main()


你的问题在于,StateMachine.__init__(self, Started())要求selfdata属性已经设置,但你只设置下一行(self.data = 10)。切换这两条线,然后再试一次!


哇,这段代码真是不必要的复杂。

问题似乎是,您在StateMachineTest类的__init__中初始化Started,以便在设置self.data之前运行其on_enter

您可以通过将self.data的赋值移到super调用之上来解决这个问题,但更好的办法是简化继承层次结构。