dwavesystems / dwavesystems/dimod

Use a more uniform structure for cython classes

Open
#1,289 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
143
Forks
91
Avg merge
1h 24m
Merged PRs (30d)
3

Description

Currently we have two patterns for Cython classes and it would be nice to be more consistent

#### Subclass pattern
Have a Cython class as a superclass of the Python class
```cython
cdef class cyClass:
...

class Class(cyClass):
...
```
examples: [cyVariables](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/cyvariables.pyx#L33) and [Variables](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/variables.py#L75), [cyConstrainedQuadraticModel](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/constrained/cyconstrained.pyx#L62) and [ConstrainedQuadraticModel](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/constrained/constrained.py#L137)

The major drawback of this pattern is it does not allow multiple data types, because Cython does not allow templated classes (nor is such a concept really meaningful).

#### Attribute pattern
Have the Python class hold the Cython class as an attribute
```cython
cdef class cyClass
...

class Class:
def __init__(self):
self.data = cyClass()
```
examples: [cyBQM](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/binary/cybqm/cybqm_template.pyx.pxi#L50) and [BQM](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/binary/binary_quadratic_model.py#L70)

This pattern gives us the ability to support multiple underlying data types, but IMO it's harder to understand and requires a lot of indirect methods like
```python
def f(self):
return self.data.f()
```
which adds clutter and maintenance and creates a performance problem (partially mititgated by [forwarding_method](https://github.com/dwavesystems/dimod/blob/b645445709df02a360d7de243320493ac733d29c/dimod/decorators.py#L335)).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.