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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:42:06+00:00 2026-06-09T22:42:06+00:00

I have 2 classes A and B: class A(object): x = 0 class B(object):

  • 0

I have 2 classes A and B:

class A(object):
    x = 0

class B(object):
    y = 0

How can I make it so B “inherits” A‘s class-level variables (x in this case) by using decorators? Is it at all possible? The desired behavior (if possible), after decorated, B would look like this:

class B(object):
    x = 0
    y = 0

Note: If anyone wants/needs to know why I’m asking this, it’s simply to make SQLAlchemy’s Concrete Table Inheritance look nicer in code, although I can see many use cases for such behavior.

  • 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-09T22:42:08+00:00Added an answer on June 9, 2026 at 10:42 pm

    Sure you can; you can use a class decorator that takes class A as an argument, then updates the decorated class for you:

    import types
    
    class copyattributes(object):
        def __init__(self, source):
            self.source = source
    
        def __call__(self, target):
            for attr, value in self.source.__dict__.items():
                if attr.startswith('__'):
                    continue
                if isinstance(value, (property, types.FunctionType)):
                    continue
                setattr(target, attr, value)
            return target
    

    The decorator copies anything that is really an attribute (not a function or a property), and doesn’t start with a double underscore.

    Usage:

    class A(object):
        x = 0
    
    @copyattributes(A)
    class B(object):
        y = 0
    

    Tested on the prompt:

    >>> class A(object):
    ...     x = 0
    ...
    >>> @copyattributes(A)
    ... class B(object):
    ...     y = 0
    ... 
    >>> B.y
    0
    >>> B.x
    0
    >>> dir(B)
    ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x', 'y']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following c# classes: class A : Object { foo() {} }
D2.0 classes have a __monitor class property that gives access to the class object's
I have the following classes. I have a object var of Description class. I
I have a Balloon class (see this ) that inherits from CCSprite . I
I have two classes like this (simplified and renamed): class Base { public: virtual
In Java, do class objects have the same heritance relations as the classes they
I have a class that contains objects of two other classes. I need one
Say I have classes class A{ //code for class A } class B{ //code
I have two classes class A { public string something { get; set; }
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived

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.