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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:44:23+00:00 2026-05-15T01:44:23+00:00

i have a question about class attribute in python. class base : def __init__

  • 0

i have a question about class attribute in python.

class base :
    def __init__ (self):
        pass
    derived_val = 1

t1 = base()
t2 = base()

t2.derived_val +=1
t2.__class__.derived_val +=2
print t2.derived_val             # its value is 2
print t2.__class__.derived_val   # its value is 3

The results are different. I also use id() function to find t2.derived_val and t2.__class__.derived_val have different memory address.
My problem is derived_val is class attribute. Why it is different in above example?
Is it because the instance of class copy its own derived_val beside the class attribute?

  • 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-15T01:44:23+00:00Added an answer on May 15, 2026 at 1:44 am

    There are class attributes, and instance attributes.
    When you say

    class base :
        derived_val = 1
    

    You are defining a class attribute. derived_val becomes a key in
    base.__dict__.

    t2=base()
    print(base.__dict__)
    # {'derived_val': 1, '__module__': '__main__', '__doc__': None}
    print(t2.__dict__)
    # {}
    

    When you say t2.derived_val Python tries to find ‘derived_val’ in t2.__dict__. Since it is not there, it looks if there is a 'derived_val' key in any of t2‘s base classes.

    print(t2.derived_val)
    print(t2.__dict__)
    # 1
    # {}
    

    But when you assign a value to t2.derived_val, you are now adding an instance attribute to t2. A derived_val key is added to t2.__dict__.

    t2.derived_val = t2.derived_val+1
    print(t2.derived_val)
    print(t2.__dict__)
    # 2
    # {'derived_val': 2}
    

    Note that at this point, there are two derived_val attributes, but only
    the instance attribute is easily accessible. The class attribute becomes accessible only through referencing base.derived_val or direct access to the class dict base.__dict__.

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

Sidebar

Related Questions

I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have a question about the position attribute, looks the <div class='bloqued'></div> , I
I have a question about __class__ in python. The documentation says that __class__ is
We just started learning about class inheritance and attribute lookup in python. I have
I have a form (edited for brevity) as follows: class InteractionForm(forms.Form): def __init__(self, *args,
I have a question about best practices in Java. I'm writing a class, which
I have a question about the 'Event Dispatch Thread'. I have a Main class
I have a question about race conditions and simultaneous writes. I have a class
I had a question about Java Class Path variables. If I have multiple jars
I have a simple question about the expression in jquery to select a class

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.