Kenly
Kenly

Reputation: 26688

setter and getter does not work

I used the following code:

class P:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value * 2

p = P()
print(p.x)
p.x = 10
print(p.x)

output: None 10

The output should be:
None 20

I want to know why this does not work.

Upvotes: 1

Views: 231

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798546

Here's why:

class P:

Descriptors only work in new-style classes.

class P(object):

Upvotes: 3

Related Questions