状态和行动形式主义。如何在不同 类 之间实现 +、-、= 运算符
State and Actions formalism. How to implement +,-,= operators between different classes
我对一组状态有疑问。要从一种状态转移到另一种状态,我需要执行一组操作。
我正在尝试以下列比较 returns 为真:
的方式实施状态和操作 类
state1 + actions == state2
state2 - actions == state1
state2 - state1 == actions
我为我的实现编写了一个测试用例,如下所示:
class State:
pass
class Actions:
pass
starting_state ={'a':'1',
'b':'2'}
some_actions = {'a': 'remove 1',
'b' : 'remove 2,add #',
'c' : 'add 5'}
inverse_actions = {'a': 'add 1',
'b' : 'remove #,add 2',
'c' : 'remove 5'}
final_state = {'b':'#',
'c':'5'}
state1 = State(starting_state)
actions = Actions(some_actions)
state2= state1 + actions
assert State(final_state)==state2
assert Actions(inverse_actions)== -actions
assert state2 - actions == state1
assert state2 - state1 == actions
我的问题是:
- 没有任何预建的东西可以帮助我实现状态和动作类?
- 如果不是,如何实现不同类之间的+和-操作?有没有办法避免循环依赖?
感谢您的帮助!
您可以为此使用运算符重载。这是一个例子:
class A:
def __init__(self, a):
self.a = a
def __add__(self, other):
return A(self.a+other.a)
def __str__(self):
return str(self.a)
i = A(1)
j = A(1)
l = i+j
assert str(l) == "2"
您可以在此处找到可以根据需要重载的运算符列表:
https://docs.python.org/3/library/operator.html
正如 Marin 所建议的,您需要使用运算符重载。这是一个适用于 +
和 ==
运算符的简单示例:
class State:
def __init__(self, s):
self.state = s
def __add__(self, other):
d = self.state.copy()
if isinstance(other, State):
d.update(other.state)
elif isinstance(other, Actions):
for var, actions in other.actions.items():
for action in actions.split(","):
statement, value = action.split()
if statement == "remove":
if d.pop(var, None) is None:
print("Cannot remove", value, ", not in state")
elif statement == "add":
d[var] = value
else:
return NotImplemented
return State(d)
def __eq__(self, other):
if isinstance(other, State):
return self.state == other.state
elif isinstance(other, dict):
return self.state == other
else:
return NotImplemented
class Actions:
def __init__(self, a):
self.actions = a
starting_state = {'a': '1',
'b': '2'}
some_actions = {'a': 'remove 1',
'b': 'remove 2,add #',
'c': 'add 5'}
inverse_actions = {'a': 'add 1',
'b': 'remove #,add 2',
'c': 'remove 5'}
final_state = {'b': '#',
'c': '5'}
state1 = State(starting_state)
actions = Actions(some_actions)
state2 = state1 + actions
assert State(final_state) == state2
assert state1 + actions == final_state
# assert Actions(inverse_actions) == -actions
# assert state2 - actions == state1
# assert state2 - state1 == actions
您需要重载 __sub__
和 __neg__
才能获得最终结果。
我对一组状态有疑问。要从一种状态转移到另一种状态,我需要执行一组操作。 我正在尝试以下列比较 returns 为真:
的方式实施状态和操作 类state1 + actions == state2
state2 - actions == state1
state2 - state1 == actions
我为我的实现编写了一个测试用例,如下所示:
class State:
pass
class Actions:
pass
starting_state ={'a':'1',
'b':'2'}
some_actions = {'a': 'remove 1',
'b' : 'remove 2,add #',
'c' : 'add 5'}
inverse_actions = {'a': 'add 1',
'b' : 'remove #,add 2',
'c' : 'remove 5'}
final_state = {'b':'#',
'c':'5'}
state1 = State(starting_state)
actions = Actions(some_actions)
state2= state1 + actions
assert State(final_state)==state2
assert Actions(inverse_actions)== -actions
assert state2 - actions == state1
assert state2 - state1 == actions
我的问题是:
- 没有任何预建的东西可以帮助我实现状态和动作类?
- 如果不是,如何实现不同类之间的+和-操作?有没有办法避免循环依赖?
感谢您的帮助!
您可以为此使用运算符重载。这是一个例子:
class A:
def __init__(self, a):
self.a = a
def __add__(self, other):
return A(self.a+other.a)
def __str__(self):
return str(self.a)
i = A(1)
j = A(1)
l = i+j
assert str(l) == "2"
您可以在此处找到可以根据需要重载的运算符列表: https://docs.python.org/3/library/operator.html
正如 Marin 所建议的,您需要使用运算符重载。这是一个适用于 +
和 ==
运算符的简单示例:
class State:
def __init__(self, s):
self.state = s
def __add__(self, other):
d = self.state.copy()
if isinstance(other, State):
d.update(other.state)
elif isinstance(other, Actions):
for var, actions in other.actions.items():
for action in actions.split(","):
statement, value = action.split()
if statement == "remove":
if d.pop(var, None) is None:
print("Cannot remove", value, ", not in state")
elif statement == "add":
d[var] = value
else:
return NotImplemented
return State(d)
def __eq__(self, other):
if isinstance(other, State):
return self.state == other.state
elif isinstance(other, dict):
return self.state == other
else:
return NotImplemented
class Actions:
def __init__(self, a):
self.actions = a
starting_state = {'a': '1',
'b': '2'}
some_actions = {'a': 'remove 1',
'b': 'remove 2,add #',
'c': 'add 5'}
inverse_actions = {'a': 'add 1',
'b': 'remove #,add 2',
'c': 'remove 5'}
final_state = {'b': '#',
'c': '5'}
state1 = State(starting_state)
actions = Actions(some_actions)
state2 = state1 + actions
assert State(final_state) == state2
assert state1 + actions == final_state
# assert Actions(inverse_actions) == -actions
# assert state2 - actions == state1
# assert state2 - state1 == actions
您需要重载 __sub__
和 __neg__
才能获得最终结果。