1

I faced a nececssity to add atribute to one of classes from cv2 library (simply id of the object). But when I try

setattr(keypoint, 'id', 1)

I get the error 'cv2.KeyPoint' object has no attribute 'id'

Whereas object of semi defined class:

class testclass:
    def __init__(self, a):
        self.a = 1
test = testclass(2)
setattr(test, 'id', 3)

works and now test.id returns 3. Why setattr does not work for cv2.KeyPoint object? Is there a way to make it work?

Nourless
  • 729
  • 1
  • 5
  • 18
  • 4
    It's possible `cv2.KeyPoint` is a C object, not a Python one, so you can't add arbitrary properties to it. You'd see the same if you tried to add them to e.g. a string. – jonrsharpe Jul 13 '21 at 15:02
  • Related: https://stackoverflow.com/questions/1285269/why-cant-you-add-attributes-to-object-in-python You can `setattr()` to it if it contains a `__dict__`. – Pranav Hosangadi Jul 13 '21 at 15:04
  • Note `keypoint.id = 1` wont work either. For reasons commented on. But it’s not just setattr. – JL Peyret Jul 13 '21 at 15:33

0 Answers0