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 7505653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:59:04+00:00 2026-05-29T21:59:04+00:00

So I have this: class Parent(object): def __init__(self, val): print ‘enter Base init’ self._set_x(val)

  • 0

So I have this:

class Parent(object):

    def __init__(self, val):
        print 'enter Base init'
        self._set_x(val)
        print 'leave Base init'

    def _get_x(self):
        return self._x

    def _set_x(self, val):
        print 'enter Base _set_x'
        self._x = val
        print 'leave Base _set_x'

    x = property(_get_x, _set_x)

class Child(Parent):

    def _set_x(self, val):
        print 'enter Child _set_x'
        y = val * 2
        super(Child, self)._set_x(y)
        print 'leave Child _set_x'

child = Child(5)
num = child.x
child.x = 5
print num == child.x

And when I run it, I get this:

enter Base init
enter Child _set_x
enter Base _set_x
leave Base _set_x
leave Child _set_x
leave Base init
enter Base _set_x
leave Base _set_x
False

I’ve been reading around, and people are saying that the overriding shouldn’t work, but my question is why is there this seeming inconsistency here? The subclass’ setter gets called when calling from init, but when you later act on the already initialized object, it calls the base’s setter. Can someone explain what is going on here?

  • 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-05-29T21:59:05+00:00Added an answer on May 29, 2026 at 9:59 pm

    Because you call it inconsistently — once directly, and once via a property. Change self._set_x(x) in Parent.__init__ to self.x = x and you’ll see Child._set_x is never called. To override a setter in a child class, you can use property.setter as decorator:

    class Child(Parent):
        @Parent.x.setter
        def x(self, arg):
            super()._set_x(arg)
    

    Or add a level of indirection to the property:

    class Parent(object):
        # ...
        x = property(
            lambda self:    self._get_x(),
            lambda self, x: self._set_x(x)
        )
    

    The overriding doesn’t work directly, because property stores concrete method objects (Parent._get_x and Parent._set_x), and doesn’t look them up again, even if the type of the object happens to change — the code that runs is the same regardless. Indirection forces lookup in the dynamic type of self, which allows for overriding.

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

Sidebar

Related Questions

I have the following situation in my python code: class Parent(object): def run(self): print
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
this code: import wx app = None class Plugin(wx.Panel): def __init__(self, parent, *args, **kwargs):
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
I have written this clone method for when the parent of the Employee class
I have a table structured like this: <table> <tr id=id1 class=parent></tr> <tr class=id1></tr> <tr
I have model class CreatedMixin(object): __abstract__ = True @declared_attr def created_by(cls): return Column(Integer, ForeignKey('user.user_id',
Simple question, I have code like this: class Context[A] { def t: A }
I have form: class Form(forms.ModelForm): id = forms.ModelChoiceField(queryset=Category.objects.all(), widget=forms.HiddenInput()) def choices(self): items = ...query
I have this class called Table: class Table { public string Name { get

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.