Skip to content

Commit 1f5eccf

Browse files
committed
refactor: turn Symbol & Dummy into cdefs
Making `Symbol` and `Dummy` into Cython extension classes should automatically turn them into slotted classes, i.e., using the `__slots__` attribute rather the `__dict__` one to store attributes.
1 parent 7b158d2 commit 1f5eccf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ cdef class Expr(Basic):
11101110
pass
11111111

11121112

1113-
class Symbol(Expr):
1113+
cdef class Symbol(Expr):
11141114

11151115
"""
11161116
Symbol is a class to store a symbolic variable with a given name.
@@ -1155,7 +1155,7 @@ class Symbol(Expr):
11551155
return self.__class__
11561156

11571157

1158-
class Dummy(Symbol):
1158+
cdef class Dummy(Symbol):
11591159

11601160
def __init__(Basic self, name=None, *args, **kwargs):
11611161
if name is None:

symengine/tests/test_symbol.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ def test_symbol():
88
assert str(x) == "x"
99
assert str(x) != "y"
1010
assert repr(x) == str(x)
11+
# Verify the successful use of slots.
12+
assert hasattr(x, "__slots__")
13+
assert not hasattr(x, "__dict__")
14+
assert not hasattr(x, "__weakref__")
1115

1216

1317
def test_symbols():
@@ -148,6 +152,7 @@ def test_has_symbol():
148152
assert not has_symbol(c, a)
149153
assert has_symbol(a+b, b)
150154

155+
151156
def test_dummy():
152157
x1 = Symbol('x')
153158
x2 = Symbol('x')
@@ -159,3 +164,7 @@ def test_dummy():
159164
assert xdummy1 != xdummy2
160165
assert Dummy() != Dummy()
161166
assert Dummy('x') != Dummy('x')
167+
# Verify the successful use of slots.
168+
assert hasattr(xdummy1, "__slots__")
169+
assert not hasattr(xdummy1, "__dict__")
170+
assert not hasattr(xdummy1, "__weakref__")

0 commit comments

Comments
 (0)