6

I'm trying to create a point class which defines a property called "coordinate". However, it's not behaving like I'd expect and I can't figure out why.

class Point:
    def __init__(self, coord=None):
        self.x = coord[0]
        self.y = coord[1]

    @property
    def coordinate(self):
        return (self.x, self.y)

    @coordinate.setter
    def coordinate(self, value):
        self.x = value[0]
        self.y = value[1]

p = Point((0,0))
p.coordinate = (1,2)

>>> p.x
0
>>> p.y
0
>>> p.coordinate
(1, 2)

It seems that p.x and p.y are not getting set for some reason, even though the setter "should" set those values. Anybody know why this is?

Kara
  • 6,115
  • 16
  • 50
  • 57
cdwilson
  • 4,310
  • 4
  • 26
  • 32
  • What happens if you try to print `p.coordinate` just after you have instantiated the object, before trying to change it? – Daniel Roseman Aug 26 '09 at 22:47
  • This is a gotcha old-style-classes-vs-new-style-classes and you might expect either `property()` or the IDE would catch it. – smci Aug 21 '12 at 22:47
  • Possible duplicate of [Property getter/setter have no effect in Python 2](https://stackoverflow.com/questions/9163940/property-getter-setter-have-no-effect-in-python-2) – ivan_pozdeev Jun 14 '18 at 07:12

2 Answers2

10

The property method (and by extension, the @property decorator) requires a new-style class i.e. a class that subclasses object.

For instance,

class Point:

should be

class Point(object):

Also, the setter attribute (along with the others) was added in Python 2.6.

Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72
4

It will work if you derive Point from object:

class Point(object):
    # ...
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662