Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend Symbol class in sympy?

I’m having trouble extending the Symbol class in sympy. It could be a result of something with class extensions in general, or it might also be an issue with this specific “Symbol” class.

I want to extend the Symbol class to have an additional attribute called “boolean_attr” which is a True/False attribute. This simulates what I’m trying to do:

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

And that seems to work as expected:

my_B = B(False)
print my_B.boolean_attr
>>>> False

So, when I try this in Sympy this is what I do:

from sympy.core.symbol import Symbol
class State(Symbol):

    def __init__(self, boolean_attr):
        self.boolean_attr = boolean_attr

But this doesn’t work:

TypeError: name should be a string, not <type 'bool'>

How do I add an attribute to the Symbol class in sympy? Thanks.

(Additionally, I should mention that this might be an xy problem without me knowing it. I want to know how to add an attribute to a class, and my question assumes that extending the class is the best way to do that. If this is an incorrect assumption, please let me know)

like image 727
makansij Avatar asked Jan 25 '26 20:01

makansij


1 Answers

Try the following code, it works for me on python 3.

from sympy.core.symbol import Symbol
class State(Symbol):
    def __init__(self, boolean_attr):
        self.boolean_attr = boolean_attr
        super()

Python 2 code :

from sympy.core.symbol import Symbol
class State(Symbol):
    def __init__(self, boolean_attr):
        self.boolean_attr = boolean_attr
        super(State, self).__init__()
like image 94
LazyCoder Avatar answered Jan 28 '26 22:01

LazyCoder