0

I have a class Com

class Com:
    def __init__(self, a: float, b: float):
        self.a = a
        self.b = b

and in the class this function:

def add(comp: "Comp", *comps: "Com") -> "Com":

with this input:

c1 = Com(5.0, 10.0)
c2 = Com(7.0, 100.0)
c_sum = Com.add(c1, c1, c2, Com(33.75, -14.25))

I don't understand how I can access the parameters of the add() function and work with them. Can someone explain it?

Dave Twickenham
  • 171
  • 1
  • 8
  • `comps` is a tuple of extra parameters passed to `add()` starting with the second one. You access it like a tuple. In order to help you more than that you'll have to post some kind of attempt to work with it so we could guide you further – roeen30 Dec 11 '22 at 17:06
  • @roeen30 Ok, but when I print the elements of the tuple I get `<__main__.Com object at 0x104497fd0>` how can I turn them into 'Com' class variables? – Dave Twickenham Dec 11 '22 at 17:23
  • What else would you expect the object to show up as…? – deceze Dec 11 '22 at 17:31
  • They are not "class variables", they are instances of the `Com` class. This is how they are printed. If you want them to look differently, for example - showing the values of `self.a` and `self.b`, you need to implement a `__repr__()` method. Alternatively, you can create a dataclass which does this for you. https://docs.python.org/3/library/dataclasses.html – roeen30 Dec 12 '22 at 10:41

0 Answers0