0

my code is probably very unconventional since I am self-taught, any help or tips are appreciated.

I tried to speed up my python code using cython and have had some very good results only using a little bit of static typing here and there. However, I think there is much room for improvement.

I'm using a few classes to do similar operations in slightly different fashion, depending on their type, like this:

class A:
    def __init__(self, A, B, C):
        self.A = A
        self.B = B
        self.C = C
        self.combined = 0

    def UpdateCombined(self):
        self.combined = A + B + C
        return

class B:
    def __init__(self, A, B, C):
        self.A = A
        self.B = B
        self.C = C
        self.combined = 0

    def UpdateCombined(self):
        self.combined = A * B * C
        return

I looked into interfaces, because they seemed like the conventional way of implementing classes with the same type of methods and attributes, but different behaviour, but I came to the conclusion, that using interfaces through inheritance in my case is just unnecessary boilerplate and not really useful in anyway (if I have a interface class with NotImplemented everywhere, it really isn't any help, is it?)

I have quite a bit operations, where I have a list of instances of these classes and want to perform the same operation on all of them:

a, b, c = A(1, 3, 2)
x, y, z = B(5, 4, 3)
object_list = [a, b, c, x, y, z]

for SomeObject in object_list:
    SomeObject.UpdateCombined()

My question now is, how could I go about cleanly improving this with cython? I think, probably use cdef class to make A and B extensions types, staticly type their arguements, that would be my first move. But how to I improve the latter code part, can I use extension types for static typing?

Thanks for any help in advance, any comments are appreciated

  • Maybe a duplicate of https://stackoverflow.com/questions/33851333/cython-how-do-you-create-an-array-of-cdef-class I think (not 100% sure exactly what you want) – DavidW Nov 27 '20 at 17:32
  • Thanks, I think that might be essentially what I am looking for. Basically, I have a list of object and the list variable is an object reference, how can I type the list variable. I'm gonna test the answer posted on the other topic come back asap, thank you – Matthias K. Nov 28 '20 at 11:23

0 Answers0