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 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

Related Questions

Please see the example code below: class A { private: class B { public:
everyone. Please see example below. I'd like to supply a string to 'schedule_action' method
Given the example source code below, is it possible for someone to see the
I'm trying to run the example Gearman worker from their documentation (see below), but
Why can't I set $_SERVER['DOCUMENT_ROOT'] as attribute? see example code class foo { private
When using a helper class with an rspec test, I can't see to use
will using a shared_ptr on class instance members clean up the new'ed objects correctly
I am working with DNA sequences of length 25 (see examples below). I have
See example: <!DOCTYPE html> <html> <head> <title>language</title> <script type=text/javascript src=http://www.google.com/jsapi> </script> </head> <body> <div
See this example! int main( int argc, char ** argv ) { int *ptr

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.