0

in Python, is there a way to use a function arg like a pointer? The below code just sets 'property' to a string inside the scope of the function. It doesn't modify the dummy object.

class Dummy:
    def __init__(self):
        self._a = None
        self._b = None

    @property
    def a(self):
        return self._a

    @a.setter
    def a(self, value):
        self._a = value

    @property
    def b(self):
        return self._b

    @b.setter
    def b(self, value):
        self._b = value

def dummy_setter(property, value):
    property = value
    print(property)

dummy = Dummy()
dummy_setter(dummy.a, "foo")
>foo
print(dummy.a)
>None
dummy_setter(dummy.b, "bar")
>bar
print(dummy.b)
>None

HapG
  • 1
  • 1
  • 1
    Why are you not just setting the values with `dummy.a = "foo"`? – RetardedHorse May 20 '20 at 11:27
  • 1
    `foo(a.b, 'c')` will never *replace* `b` with `'c'`. It's not possible. At the very least you need to do `foo(a, 'b', 'c')`. Which is essentially what `setattr` is. – deceze May 20 '20 at 11:28
  • @RetardedHorse dummy_setter is a Qt widget that modifies an object's properties. I want to make it reusable by passing the properties I want the widget to modify as arguments. – HapG May 20 '20 at 11:35

0 Answers0