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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:17:56+00:00 2026-06-12T20:17:56+00:00

Is it possible to conditionally override a class property with a property method? If

  • 0

Is it possible to conditionally override a class property with a property method?

If I have this class which I can instantiate by passing in a dict:

def Foo(Object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    # pseudo code (this doesn't work)
    if not self.bar:
        @property
        def bar(self):
            return u"I have overridden foo's bar"

And I make this instance and set bar to '' or None:

my_foo = Foo(**{'bar':u''})

and then I call the bar property:

my_foo.bar

and get

u"I have overridden foo's bar"

I would like to have the property method bar returned, instead of the bar value that was passed in when the object was created.

Can I do that somehow? Any help would be awesome.

  • 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-12T20:17:56+00:00Added an answer on June 12, 2026 at 8:17 pm

    To override a property you have to act on the class and not on the instance, because their machinery, on the instance, gets called before __dict__ lookup and you end up with AttributeErrors. Instead you can set a different property on the class.

    But to do so you either have to modify your class every time you create an instance(which I bet you do not want), or you have to generate new classes dynamically.

    For example:

    class Foo(object):
        def __init__(self, val):
            self._val = val
        @property
        def val(self):
            return self._val
    
    class SubType(Foo):
        def __new__(cls, val):
            if val % 2:
                #random condition to change the property
                subtype = type('SubFoo', (SubType,),
                               {'val': property((lambda self: self._val + 1))})
                    return object.__new__(subtype)
                else:
                    return object.__new__(cls)
    

    And the results are:

    >>> d = SubType(3)  #property changed
    >>> d.val
    4
    >>> f = SubType(2)  #same property as super class
    >>> f.val
    2
    

    I don’t like much this kind of hacks. Probably the easier way of doing thing is calling a private method that computes the property value, for example:

    class Foo(object):
        def __init__(self, val):
            self._val = val
        def _compute_val(self):
            return self._val
        @property
        def val(self):
            return self._compute_val()
    
    class SubFoo(Foo):
        def _compute_val(self):
            if self._val % 2:
                    return self._val + 1
            else:
                    return self._val
    

    Which yields the same results as before:

    >>> d = SubFoo(3)
    >>> d.val
    4
    >>> f = SubFoo(2)
    >>> f.val
    2
    

    I believe this trick could be seen as an application of the Template Method design pattern, even though it is applied to properties.

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

Sidebar

Related Questions

Possible Duplicate: How can I conditionally compile my C# for Mono vs. Microsoft .NET?
Possible Duplicate: How to print out the method name and line number and conditionally
Is it possible in a .NET app (C#), to conditionally detect if a class
Can I use REPLACE function conditionally? e.g. I have a query: SELECT diag_type FROM
Is it possible to conditionally hide or disable functions in a template class using
Is it possible to conditionally set a CSS class or encode a string to
I read this question , which highlights a solution to conditionally insert values into
Is it possible to ask fabric to perform actions conditionally? This should be fairly
I am trying to learn about all possible conditionals that can be used on
Is it possible for me to have a conditional in XSLT such that I

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.