-1

I am trying below program, getting below error:

TypeError: set_a() missing 1 required positional argument: 'value'

CODE :

class A:
    def __init__(self):
        pass
    @property
    def a(self,value):
        return value

    @property
    def set_a(self, value):
        self.a(value)

    def call_req(self,value):
        return self.set_a(value)

aobj = A()
aobj.call_req(5)

Tried the solution given here, Didn't worked out.

Can someone explain Why I am getting the error, or is this something totally wrong I am trying ?

coder3521
  • 2,608
  • 1
  • 28
  • 50

1 Answers1

1

A @property is something that returns a value. You never call a @property, the call is implicit in accessing it as an attribute. self.set_a is trying to call set_a to return a value, which you're then trying to call with the parentheses after. So there's an implicit call before the call you think you're making, which is missing the argument. You probably want to declare the property like this:

class A:
    def __init__(self):
        self._foo = 42

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

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

obj = A()
obj.a = 5
deceze
  • 510,633
  • 85
  • 743
  • 889