pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.52k stars 525 forks source link

Example in non-quick start fails #396

Closed salmanisd closed 4 years ago

salmanisd commented 4 years ago
Traceback (most recent call last):
...
NameError: name 'State' is not defined
salmanisd commented 4 years ago

Missing "State" in from transitions import Machine

aleneum commented 4 years ago

Hi @salmanisd,

thank you for taking the time to open an issue and also creating a PR. However, I cannot reproduce your issue. Combining Matter and the construction of Machine results in something like this:

from transitions import Machine

class Matter(object):
    pass

lump = Matter()
machine = Machine(model=lump, states=['solid', 'liquid', 'gas', 'plasma'], initial='solid')

# Lump now has state!
lump.state

where State shouldn't be needed.

In the passage about the State class (after "The following snippets illustrate several ways to achieve the same goal: ") we need to import it though. I added a line in the code example here which hopefully clarifies the need for an import there.

salmanisd commented 4 years ago

Hi aleneum,

My issue was that I was trying the different example code blocks in the same file going step by step so while the above block you mentioned didnt give me an error, the next blocks involving "State" will give an error. So I thought for noobs like me it would be better if all the required modules for just imported at once in the start. You can close this issue and the PR.

aleneum commented 4 years ago

@salmanisd,

So I thought for noobs like me it would be better if all the required modules for just imported at once in the start. You can close this issue and the PR.

I see. yeah, that's probably a matter of taste. nevertheless it's definitely worth to report and also to clarify this. I have to rely on people like you to find these issues because I hardly read the documentation myself. So thanks again for your time and effort!