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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:28:08+00:00 2026-06-17T15:28:08+00:00

I was trying to override a member of a Python (2.7) class with a

  • 0

I was trying to override a member of a Python (2.7) class with a property, as shown in the following code:

class Base:
    def __init__(self):
        self.foo = 1

class Derived(Base):
    foo = property(lambda self: 2)

print Derived().foo

However, the last line prints 1 instead of 2. From the way I thought properties are supposed to work (ie., easily change a member to a function later on), this seems counter-intuitive to me. Am I missing something? Is there some workaround?

  • 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-17T15:28:09+00:00Added an answer on June 17, 2026 at 3:28 pm

    This doesn’t work because you aren’t using a new-style class. Properties are descriptors which only work on new-style classes. What your code is doing is this:

    You create a class Derived with a class attribute foo. Then when you create an instance of the class, Base.__init__ takes over since Derived has no __init__ and you add the instance attribute foo which takes precedence to the class attribute.

    If you change:

    class Base: #old style class
    

    to:

    class Base(object):  #new style class
    

    You’ll run into an entirely new problem, mainly that your property doesn’t have an appropriately defined setter, so when you do self.foo = 1 in Base.__init__ you’ll get an AttributeError

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

Sidebar

Related Questions

I'm trying to override a property in a base class with a different, but
Heres my code where im tryng to override SettingsFileName member: public class ProcessorTest: Processor
im trying to override the add() but it will not compile public class AVLTree<E
Okay, so I'm trying to override a function in a parent class, and getting
I believe, a derived class can override only those functions which it inherited from
I'm trying to set up an inheritance hierarchy similar to the following: abstract class
I am trying to mock a class like so (thank's google for code design
I'm trying to create an abstract class that defines a property with a getter.
The following code is what I'm trying to use to print data to the
I'm trying to transform the following XML.. <?xml version=1.0 encoding=utf-16?><Member TextRank=unknown FullName=My Name ..etc..

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.