【发布时间】:2019-12-05 06:33:51
【问题描述】:
我无法在 sympy 中扩展 Symbol 类。这可能是一般类扩展的结果,也可能是这个特定的“符号”类的问题。
我想扩展 Symbol 类,使其具有一个名为“boolean_attr”的附加属性,它是一个 True/False 属性。这模拟了我正在尝试做的事情:
class A(object): # This simulates what the "Symbol" class is in sympy
__slots__ = ['a']
def __init__(self, a):
self.a = a
# this simulates my extension to add a property
class B(A):
def __init__(self, boolean_attr):
self. boolean_attr = boolean_attr
这似乎按预期工作:
my_B = B(False)
print my_B.boolean_attr
>>>> False
所以,当我在 Sympy 中尝试这个时,我就是这样做的:
from sympy.core.symbol import Symbol
class State(Symbol):
def __init__(self, boolean_attr):
self.boolean_attr = boolean_attr
但这不起作用:
TypeError: name should be a string, not <type 'bool'>
如何在 sympy 中为 Symbol 类添加属性?谢谢。
(另外,我应该提一下,这可能是xy problem,而我并不知道。我想知道如何为类添加属性,我的问题假设 扩展 类是最好的方法。如果这是一个不正确的假设,请告诉我)
【问题讨论】:
-
请添加必要的导入以便我们运行代码和调试。
-
当然好点。固定
-
下面介绍的解决方案能解决您的问题吗?
-
没有解决问题。
标签: python python-2.7 sympy python-object