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

The Archive Base Latest Questions

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

See example below. Using the __class__ prefix with the class instance ‘object’ gives the

  • 0

See example below. Using the __class__ prefix with the class instance ‘object’ gives the expected result. Why is the class variable even available at the class instance ‘c()’ without the __class__ prefix? In what situation is it used?

>>> class c:
        x=0


>>> c.x
0
>>> c().x
0
>>> c().__class__.x
0
>>> c.x += 1
>>> c.x
1
>>> c().x += 1
>>> c.x
1
>>> c().__class__.x += 1
>>> c.x
2
>>> 
  • 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-16T01:38:51+00:00Added an answer on May 16, 2026 at 1:38 am

    Why is the class variable even
    available at the class instance ‘c()’
    without the class prefix?

    You can usefully think of it as instances “inheriting from” their class. IOW, when an attribute named 'atr' is looked up on the instance x (e.g. by x.atr), unless found in the instance itself it’s next looked up in the class (which in turn may cause lookup in the class’s bases, up the mro chain).

    In what situation is it used?

    Most common single case:

    class sic(object):
      def foo(self): ...
    
    x = sic()
    x.foo()
    

    You may not think of foo as a “class variable”, but that’s because the term “variable” is really pretty meaningless in Python, which rather deals with names and attributes. There is no separate namespace for callable and non-callable objects at a given lexical scope: they all share the same namespace.

    So, for example, if you did x.foo = 23 then you couldn’t call x.foo() any more — because the lookup for x.foo would give you the value 23, and that’s an int, not callable.

    Another way to look at this is that

    class one(object):
      foo = lambda self: ...
    
    class two(object):
      def foo(self): ...
    
    class three(object):
      foo = 23
    

    present no deep difference — they’re all way to set class attribute foo (one is not callable, two are; one is bound with a def statement, two with assignments; there differences are not deep in terms of attribute lookup on instances of these classes).

    A typical use for non-callable attributes might be:

    class zap(zop):
      zep = None
    
      def blub(self):
        if self.zep is None: self.zep = 23
      ...&c...
    

    No need to give zap an __init__ with self.zep = None and a delegation to the superclass’s __init__ (if any) — simpler to just inherit zop‘s __init__ (if any) and use a class attribute as long as feasible (it will become an instance attribute instead if and only if blub is called on that instance — also may save a tiny bit of memory for instances on which blub is never called;-).

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

So Im trying to make an object dynamically, please see below code from my
EDIT: See my working code in the answers below. In brief: I have a
I would like to understand what's happening in the example below (where a protected
I see endless examples that involve Url.Action in a view to generate a URL
As a general rule, I prefer using value rather than pointer semantics in C++
I am connecting to a Java Axis2 web service using a .NET web service
For example, if I have two files: file1: This is file 1 and file2:
I consider fluent interfaces very convenient for many tasks. But I feel uneasy when
Say you have two classes, A and B. Is it possible to instantiate both
In response to another question I asked about regular expressions, I was told to

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.