Why does this python decorator refuse to set the setter?

46 views Asked by At

This is my code:

class Property:
    def __init__(self, fget, fset):
        self.fget = fget
        self.fset = fset

    def __get__(self, obj, objtype=None):
        return self.fget(obj)
    
    def __set__(self, obj, value):
        self.fset(obj, value)

class X:
    def __init__(self, val):
        self.__x = val

    @Property
    def x(self):
        return self.__x

    @x.setter
    def x(self, val):
        self.__x = int(val)

myx = X(13)
print(myx.x)

I get this error:

Traceback (most recent call last):
  File  "/home/izilinux/git/python/property/ex2.py", line 21, in <module>
    class X:
File "/home/izilinux/git/python/property/ex2.py", line 26, in X
    def x(self):
TypeError: Property.__init__() missing 1 required positional argument: 'fset'

The getter works fine! The problem started once i tried encorporating a setter into the code. Any ideas?

0

There are 0 answers