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

  • Home
  • SEARCH
  • 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 7845071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:06:22+00:00 2026-06-02T17:06:22+00:00

In Python, class variables can be accessed via that class instance: >>> class A(object):

  • 0

In Python, class variables can be accessed via that class instance:

>>> class A(object):
...     x = 4
...
>>> a = A()
>>> a.x
4

It’s easy to show that a.x is really resolved to A.x, not copied to an instance during construction:

>>> A.x = 5
>>> a.x
5

Despite the fact that this behavior is well known and widely used, I couldn’t find any definitive documentation covering it. The closest I could find in Python docs was the section on classes:

class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'

[snip]

… By definition, all attributes of a class that are function objects define corresponding methods of its instances. So in our example, x.f is a valid method reference, since MyClass.f is a function, but x.i is not, since MyClass.i is not. …

However, this part talks specifically about methods so it’s probably not relevant to the general case.

My question is, is this documented? Can I rely on this 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-02T17:06:24+00:00Added an answer on June 2, 2026 at 5:06 pm

    Refs the Classes and Class instances parts in the Python data model documentation

    A class has a namespace implemented by a dictionary object. Class
    attribute references are translated to lookups in this dictionary,
    e.g., C.x is translated to C.__dict__["x"] (although for new-style classes in particular there are a number of hooks which allow for other means of locating attributes).

    …

    A class instance is created by calling a class object (see above). A
    class instance has a namespace implemented as a dictionary which is
    the first place in which attribute references are searched. When an
    attribute is not found there, and the instance’s class has an
    attribute by that name, the search continues with the class
    attributes.

    Generally, this usage is fine, except the special cases mentioned as "for new-style classes in particular there are a number of hooks which allow for other means of locating attributes".

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

Sidebar

Related Questions

I've been trying to understand Python's handling of class and instance variables. In particular,
Is there a way to transfer a new class instance (python class that inherits
So I know that in Python variables declared in a method are not visible
I would like to create a class that behaves as a python variable but
Suppose I have a Python class that I want to add an extra property
As part of some WSGI middleware I want to write a python class that
<?python class += 1 ?> One really simple line of code which definitely should
In PHP one can create a reference variable, so that two named variables can
Possible Duplicate: private members in python I've got few variables I really want to
Possible Duplicate: Static class variables in Python What is the Python equivalent of static

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.