168

So, I'm playing with decorators in Python 2.6, and I'm having some trouble getting them to work. Here is my class file:

class testDec:

    @property
    def x(self): 
        print 'called getter'
        return self._x

    @x.setter
    def x(self, value): 
        print 'called setter'
        self._x = value

What I thought this meant is to treat x like a property, but call these functions on get and set. So, I fired up IDLE and checked it:

>>> from testDec import testDec
from testDec import testDec
>>> t = testDec()
t = testDec()
>>> t.x
t.x
called getter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "testDec.py", line 18, in x
    return self._x
AttributeError: testDec instance has no attribute '_x'
>>> t.x = 5
t.x = 5
>>> t.x
t.x
5

Clearly the first call works as expected, since I call the getter, and there is no default value, and it fails. OK, good, I understand. However, the call to assign t.x = 5 seems to create a new property x, and now the getter doesn't work!

What am I missing?

1
  • 6
    Please Name Classes With A Uppercase Letter. you can use lowercase for variables and module files.
    – S.Lott
    Commented Feb 28, 2009 at 14:45

4 Answers 4

321

You seem to be using classic old-style classes in python 2. In order for properties to work correctly you need to use new-style classes instead (in python 2 you must inherit from object). Just declare your class as MyClass(object):

class testDec(object):

    @property
    def x(self): 
        print 'called getter'
        return self._x

    @x.setter
    def x(self, value): 
        print 'called setter'
        self._x = value

It works:

>>> k = testDec()
>>> k.x
called getter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/devel/class_test.py", line 6, in x
    return self._x
AttributeError: 'testDec' object has no attribute '_x'
>>> k.x = 5
called setter
>>> k.x
called getter
5
>>> 

Another detail that might cause problems is that both methods need the same name for the property to work. If you define the setter with a different name like this it won't work:

@x.setter
def x_setter(self, value):
    ...

And one more thing that is not completely easy to spot at first, is the order: The getter must be defined first. If you define the setter first, you get name 'x' is not defined error.

3
  • 5
    In Python3 it's necessary no more - "classic" classes are just ok with setters :-)
    – Eenoku
    Commented Jun 12, 2015 at 9:16
  • 22
    It works in python 3 because every class is a new-style class, there are no 'classic' classes anymore.
    – craigds
    Commented Jun 23, 2015 at 23:02
  • I think one should be get a warning/error, if the decorator is not processed correctly in this case.
    – stfn
    Commented Jul 19, 2019 at 13:57
92

Just a note for other people who stumble here looking for this exception: both functions need to have the same name. Naming the methods as follows will result in an exception:

@property
def x(self): pass

@x.setter
def x_setter(self, value): pass

Instead give both methods the same name

@property
def x(self): pass

@x.setter
def x(self, value): pass

It is also important to note that the order of the declaration matters. The getter must be defined before the setter in the file or else you will get a NameError: name 'x' is not defined

4
  • 2
    This is probably going to become the "new" best answer, with more and more people on Python 3...
    – trpt4him
    Commented Jul 9, 2016 at 19:49
  • 7
    This answer is great. I think that to be complete, it would be great if you can remark that the order is important, that is, the getter must be before the setter in the code. Otherwise, you would get a NameError: name 'x' is not defined exception.
    – eguaio
    Commented Jul 18, 2016 at 13:28
  • Thanks for this. I just wasted the best part of a day on this. It never occurred to me that the methods had to be called the same thing. Really frustrating little idiom!
    – ajkavanagh
    Commented Oct 25, 2016 at 19:51
  • To understand the "why" behind this answer, read the definitive documentation here: docs.python.org/2/library/functions.html#property Especially note the "exactly equivalent" part. Commented Mar 21, 2018 at 22:48
25

You need to use new-style classes which you do by deriving your class from object:

class testDec(object):
   ....

Then it should work.

1
  • Everything above was in place. This was required for me in python 2.7.x for properties in classes to properly kick. Commented Oct 18, 2018 at 20:04
15

In case anybody comes here from google, in addition to the above answers I would like to add that this needs careful attention when invoking the setter from the __init__ method of your class based on this answer Specifically:

class testDec(object):                                                                                                                                            

    def __init__(self, value):
        print 'We are in __init__'
        self.x = value # Will call the setter. Note just x here
        #self._x = value # Will not call the setter

    @property
    def x(self):
        print 'called getter'
        return self._x # Note the _x here

    @x.setter
    def x(self, value): 
        print 'called setter'
        self._x = value # Note the _x here

t = testDec(17)
print t.x 

Output:
We are in __init__
called setter
called getter
17
2
  • Not just from the __init__ method, but fron any other method in the class.
    – Mia
    Commented Apr 2, 2018 at 3:11
  • If you are in Python3 then you should use print("xxxxx") instead print "xxxxx"
    – baponkar
    Commented May 7, 2021 at 17:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.