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

  • SEARCH
  • Home
  • 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 8629075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:42:42+00:00 2026-06-12T08:42:42+00:00

In the docs explaining properties, it is said: Be sure to give the additional

  • 0

In the docs explaining properties, it is said:

Be sure to give the additional functions the same name as the original property (x in this case.)

I.e., the getter, setter and deleter methods must all have the same name.

Why? And also, Python forbids method overloading, doesn’t it?

EDIT:
Why does the following code fail when run in Python 2.6?

class Widget(object):
    def __init__(self, thing):
        self.thing = thing
        print self.thing

    @property
    def thing(self):
        return self._thing

    @thing.setter
    def set_thing(self, value):
        self._thing = value


if __name__ == '__main__':
    Widget('Some nonsense here')  

Its output is:

 Traceback (most recent call last):   
 File "widget.py", line 16, in <module>
     Widget('Some nonsense here')     
 File "widget.py", line 3, in __init__
     self.thing = thing 
 AttributeError: can't set attribute

The code works fine when set_thing() is renamed to thing().

  • 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. Editorial Team
    Editorial Team
    2026-06-12T08:42:44+00:00Added an answer on June 12, 2026 at 8:42 am

    Python does indeed not feature method overloading, but you’re correct that the docs encourage you to name the getters and setters identically, and demonstrates it to boot. What’s going on here?

    The trick is to understand how method decorators work in Python. Whenever you see something like this:

    @foo
    def bar():
        frob()
        quux()
    

    what Python actually does under the covers is to rewrite it this way:

    def bar():
        frob()
        quux()
    bar = foo(bar)
    

    In other words: define a function bar, then replace it with the result of calling foo on the bar function.

    While that’s all true, the order of evaluation is actually slightly different than the above when it comes to how names are resolved. It might be easier for you to pretend for a moment that what actually happens looks a bit closer to this:

    def __secret_internal_function_name__():
        frob()
        quux()
    bar = foo(__secret_internal_function_name__)
    

    Why does this matter? Let’s look at the Python in that documentation link:

    class C(object):
        def __init__(self):
            self._x = None
    
        @property
        def x(self):
            """I'm the 'x' property."""
            return self._x
    
        @x.setter
        def x(self, value):
            self._x = value
    
        @x.deleter
        def x(self):
            del self._x
    

    Let’s rewrite this using what we now know to understand what Python is actually doing:

    class C(object):
        def __init__(self):
            self._x = None
    
        def __secret_x_prop__(self):
            """I'm the 'x' property."""
            return self._x
        x = property(__secret_x_prop__)
    
        def __secret_x_setter__(self, value):
            self._x = value
        x = x.setter(__secret_x_setter__)
    
        def __secret_x_getter__(self):
            del self._x
        x = x.deleter(__secret_x_getter__)
    

    Now, we can actually see what’s going on: we’re not overloading functions; we’re gradually building up a property object that references other functions.

    It’s also worth noting that, depending on how you create the property, the names don’t need to match. Specifically, if you create the property explicitly with the property function, as is done in the first example in the documentation, the names can be whatever you want; the example has them called getx, setx, and delx, and it works fine. In what I assume is a safety precaution, property.setter, property.deleter, and the like do require the function passed have the same name, but they’re doing the same thing behind the scenes as the more explicit property example.

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

Sidebar

Related Questions

I dam trying to do this: http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name Using this style This is saved as
The docs on this are pretty shoddy. There are a number of events you
MSDN docs say that only value types need boxing, but this does not apply
Where in the Google Maps API docs can I find a table explaining the
Docs,forums etc. keeps referencing the Spring petclinic example. Where can I get this for
The docs say: + (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize The fully specified name of the
Rails docs have this information for Object#blank? An object is blank if it’s false,
http://docs.djangoproject.com/en/dev/topics/logging/#topic-logging-parts-loggers I'm reading this doc. What happens when you log something? Where does it
The docs say that calling CTTypesetterCreateLine is the same as calling CTTypesetterCreateLineWithOffset with offset
http://docs.djangoproject.com/en/dev/howto/static-files/ This suggests that I can use STATIC_URL in my template to get the

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.