Skip to content

constituent/state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

state

Module to facilitate the use of state design pattern in python.
Require: python 3.4+ ##Basic usage:

from state import Stateful, State
  
class Window(Stateful):
	class ActiveState(State):
		default = True
		def rightClick(self):
			print('right clicked')
	class InactiveState(State):
		def rightClick(self):
			pass

window = Window()
window.rightClick()
window.state = window.InactiveState
window.rightClick()

Note that the first argument of rightClick is named self here, but it’s not the instance of ActiveState or InactiveState. In fact it’s the instance of Window, and subclasses of State cannot be instantiated at all. You may name it owner or something to avoid confusion, but I think self is a more practical name.

Implementation detail: all 'normal' methods in subclass of State are implicitly staticmethod, and the instance of the owner class is passed as the first argument automatically.

See example1 and example2 in the source code for more practical use cases.

About

python module for state design pattern

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages