Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:40+00:00 2026-05-11T08:50:40+00:00

So, I’m playing with decorators in Python 2.6, and I’m having some trouble getting

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:50:40+00:00Added an answer on May 11, 2026 at 8:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 71k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The way to stretch it to the same size as… May 11, 2026 at 1:21 pm
  • added an answer This is fairly easy as long as you use Expression<Action>… May 11, 2026 at 1:21 pm
  • added an answer You've implemented the Anemic Domain Model antipattern. Classes should implement… May 11, 2026 at 1:21 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.