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

The Archive Base Latest Questions

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

I am relatively new to Python and was hoping someone could explain the following

  • 0

I am relatively new to Python and was hoping someone could explain the following to me:

class MyClass:
  Property1 = 1
  Property2 = 2

print MyClass.Property1 # 1
mc = MyClass()
print mc.Property1 # 1

Why can I access Property1 both statically and through a MyClass instance?

  • 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-15T16:23:36+00:00Added an answer on May 15, 2026 at 4:23 pm

    The code

    class MyClass:
      Property1 = 1
    

    creates a class MyClass which has a dict:

    >>> MyClass.__dict__
    {'Property1': 1, '__doc__': None, '__module__': '__main__'}
    

    Notice the key-value pair 'Property1': 1.
    When you say MyClass.Property1, Python looks in the dict MyClass.__dict__ for the key Property1 and if it finds it, returns the associated value 1.

    >>> MyClass.Property1
    1
    

    When you create an instance of the class,

    >>> mc = MyClass()
    

    a dict for the instance is also created:

    >>> mc.__dict__
    {}
    

    Notice this dict is empty. When you say mc.Property1, Python first looks in mc.__dict__ for the 'Property1' key. Since it does not find it there, it looks in the dict of mc‘s class, that is, MyClass.__dict__.

    >>> mc.Property1
    1
    

    Note that there is much more to the story of Python attribute access. (I haven’t mentioned the important rules concerning descriptors, for instance.) But the above tells you the rule for most common cases of attribute access.

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

Sidebar

Related Questions

Relatively new to Python, and I saw the following construct in the PyFacebook library
Being relatively new to Python 2, I'm uncertain how best to organise my class
I am relatively new to Python, and I am experimenting with writing the following
I'm relatively new to Python and am having problems programming with Scapy, the Python
I'm relatively new to the Python world, but this seems very straight forward. Google
I'm a relatively new convert to Python. I've written some code to grab/graph data
Relatively new to python. I recently posted a question in regards to validating that
I am relatively new to python and app engine, and I just finished my
I'm still relatively new to Python, so if this is an obvious question, I
I am relatively new to Python. I am looking to create a settings module

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.