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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:41:45+00:00 2026-05-22T15:41:45+00:00

Given an arbitrary object: class Val(object): def __init__(self): this_val = 123 I want to

  • 0

Given an arbitrary object:

class Val(object):
    def __init__(self):
        this_val = 123

I want to create an abstract base class which has an attribute that is a Val():

class A(object):
    foo = Val()

I would expect that when my children inherit from that class, they would get copies of Val(). For example:

class B(A):
    pass
class C(A):
    pass

I would expect the following behavior:

>>> b = B()
>>> c = C()
>>> c.foo.this_val = 456
>>> b.foo.this_val
123

But instead I get:

>>> b.this_val
456

I understand that I could just self.foo = Val() into the init to achieve that behavior, but I have a requirement that foo remain an attribute (it is a model manager in django). Can anyone suggest a work around for this?

EDIT: I really need to be able to access the value as a class attribute, so my desired behavior is:

>>> C.foo.this_val = 456
>>> B.foo.this_val
123
  • 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-22T15:41:46+00:00Added an answer on May 22, 2026 at 3:41 pm

    Maybe using a descriptor would suit your requirements:

    class Val(object):
        def __init__(self):
            self.this_val = 123
    
    class ValDesc(object):
        def __init__(self):
            self.cls_lookup = {}
    
        def __get__(self, obj, objtype=None):
            return self.cls_lookup.setdefault(objtype, Val())
    
    class A(object):
        foo = ValDesc()
    
    class B(A):
        pass
    class C(A):
        pass
    

    Now, as long as you make sure you don’t set the instance attribute “foo” of any of your objects, they will have a class attribute that is individual to each subclass:

    b = B()
    c = C()
    cc = C()
    c.foo.this_val = 456
    print c.foo.this_val   # 456
    print cc.foo.this_val  # 456
    print b.foo.this_val   # 123
    

    EDIT: With the edit I made some hours ago, changing the key in __get__ to be objtype instead of obj.__class__, this also works when accessing the class attributes directly:

    print B.foo.this_val   # 123
    print C.foo.this_val   # 456
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an arbitrary string, what is an efficient method of finding duplicate phrases? We
Given an arbitrary sequence of points in space, how would you produce a smooth
Given any arbitrary, one-line string, my goal is to render it into a bitmap
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Given a Python object of any kind, is there an easy way to get
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a DateTime representing a person's birthday, how do I calculate their age in
Given an absolute or relative path (in a Unix-like system), I would like to
Given 2 rgb colors and a rectangular area, I'd like to generate a basic

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.