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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:50:38+00:00 2026-05-27T14:50:38+00:00

Python version 2.7. Scenario class A(object): x = 0 class B(A): pass ai =

  • 0

Python version 2.7.

Scenario

class A(object):
    x = 0

class B(A):
    pass

ai = A()
bi = B()

#Then:
id(ai.x) == id(bi.x)
>>> True

What I’d like to know is if there is a way, other than having all the class member definitions in __init__ to have instances of class B have their own copies of x without having to redefine them in class B?
Perhaps some type() tricks?

Haven’t found anything yet, but I’ll keep looking, figured here would be the best place to find an answer.
Any insight is greatly appreciated.

edit: grammars

edit2 To clarify as I didn’t really use the best example.

class Y(object):
    def __init__(self):
         self.z = 0

class A(object):
    x = Y()

class B(A):
    pass

ai = A()
bi = B()

id(ai.x) == id(bi.x)
>>> True
ai.x.z = 3
id(ai.x) == id(bi.x)
>>> True

Now the issue occurs that as I don’t reassign x in class B they both point to the same instance of class Y even if members of the instance of class Y change.

If you’re familiar with Django I’m almost recreating how their Forms work. I didn’t decide to go with a metaclass for the forms and then build up a dictionary of the fields everytime a new instance is created, perhaps I will need to switch to using a metaclass.

edit the third I’m realising now the impossibility I’m asking of the interpreter. As once I assign an instance of a class to a member there’s no way for the inherited classes to know how to create a new instance of that class. Therefore for my problem it seems metaclasses are the only solution.

  • 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-27T14:50:38+00:00Added an answer on May 27, 2026 at 2:50 pm

    For your example, it doesn’t matter with immutable objects. bi references a new object if it is ever assigned.

    class A(object):
        x = 0
    
    class B(A):
        pass
    
    ai = A()
    bi = B()
    
    print id(ai.x) == id(bi.x)
    ai.x=3
    print id(ai.x) == id(bi.x)
    

    Result:

    True
    False
    

    Note also, that even if you initialize x in __init__, that due to implementation-defined behavior, immutable objects can be cached and reused, and instances ai and bi still share the 0 object.

    class A(object):
        def __init__(self):
            self.x = 0
    
    class B(A):
        pass
    
    ai = A()
    bi = B()
    
    print id(ai.x) == id(bi.x)
    ai.x=3
    print id(ai.x) == id(bi.x)
    

    Result (with CPython 2.7.2):

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

Sidebar

Related Questions

Is there any way to know by which Python version the .pyc file was
Is there a standard way to associate version string with a Python package in
Does python have a function like call_user_func() in PHP? PHP Version: call_user_func(array($object,$methodName),$parameters) How do
I would like to pass some options to Python (version 2.6) every time, not
I'm having trouble setting up the correct python version for mod_wsgi / django setup.
(Python version: 3.1.1) I am having a strange problem with StringVar in tkinter. While
Is there a Python version of the following if - else statement in C++
I would like to run some code on the Python version of Google App
I am running Cygwin Python version 2.5.2. I have a three-line source file, called
If I was going to start an open source project using Python what version

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.