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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:17:37+00:00 2026-05-27T18:17:37+00:00

In my endeavours as a python-apprentice i got recently stuck at some odd (from

  • 0

In my endeavours as a python-apprentice i got recently stuck at some odd (from my point of view) behaviour if i tried to work with class attributes. I’m not complaining, but would appreciate some helpful comments to shed some light on this issue.

To reduce a complex matter into a more concise question i would formulate it like this:

What is the “pythonic” way to ensure that a class-attribute behaves more like a static variable in an inheritance tree?

It seems to me like a class-attribute behaves like a “copy on read” default value with polymorphic characteristics. As long as i do “read-only” operations it stays a “singleton”,
but as soon, as i access the class-attribute with an assignment through the derived class or instance it gets morphed into a new reference loosing the relation to the inherited base-reference.

(It has sure potential for some interessting features, but you have to understand it to
embrace it, so some insight is highly appreciated.)

class A(object):
    classvar = 'A'
    def setclassvar(self, value):
        A.classvar = value                   
    def __str__(self):
        return "%s id(%s) " %(A.classvar, hex(id(A.classvar))[2:-1].upper())

class A1(A):
    pass

class B(object):
    classvar = 'B'
    def setclassvar(self, value):
        self.__class__.classvar = value            
    def __str__(self):
        cvar = self.__class__.classvar
        return "%s id(%s) " %(cvar, hex(id(cvar))[2:-1].upper())

class B1(B):
    def setclassvar(self, value):
        self.__class__.classvar = value

a, a1 = A(), A1()
a1.setclassvar('a')
print "new instance A: %s" %a
print "new instance A1: %s" %a

b, b1 = B(), B1()
b1.setclassvar('bb')
print "new instance B: %s" %b
print "new instance B1: %s" %b1

a1.setclassvar('aa')
print "new value a1: %s" %a
print "new value a: %s" %a

a1.classvar = 'aaa'
print "direct access a1: %s id(%s)" %(a1.classvar, hex(id(a1.classvar))[2:-1].upper())
print "method access a1: %s" %a1
print "direct access a: %s" %a

produces the following:

new instance A: a id(B73468A0) 
new instance A1: a id(B73468A0) 
new instance B: B id(B73551C0) 
new instance B1: bb id(AD1BFC) 
new value a1: aa id(AD1BE6) 
new value a: aa id(AD1BE6) 
direct access a1: aaa id(A3A494)
method access a1: aa id(AD1BE6) 
direct access a: aa id(AD1BE6)

So either the direct (assigning) access object.classvar or mediated through self.__class__.classvar are not the same as BASECLASS.classvar.

Is this a scope issue or somethin totaly different.

Looking forward to your answers and thanks in forward. 🙂


Edit: There was an answer for a very short time suggesting the use of class-descriptors like:
How to make a class property?.

Unfortunatly that doesn’t seem to work:

class Hotel(Bar):
    def __init__(self):        
        Hotel.bar += 1

hotel = Hotel()
assert hotel.bar == 51
assert hotel.bar == foo.bar

The 2nd assertion fails! hotel.bar doesn’t reference the same object as foo.bar and hotel.bar references somethin other then Hotel.bar!


2nd Edit: I’m quite aware that singletons are considered an “antipattern” and i didn’t intend to use them (extensivly). Therefore i didn’t mention them in the question-titel. Even so there are many solutions discussing and providing solutions with and about singletons, my question stays: Why can a class-variable detach it’s reference so easily? Ruby behaves more the way it feels natural to me: http://snippets.dzone.com/posts/show/6649

  • 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-27T18:17:38+00:00Added an answer on May 27, 2026 at 6:17 pm

    If the implementation is hard to explain, it’s a bad idea.

    In this case, I am including an implementation, for sake of completness, and for this being the kind of tricky thing I like in Python.

    Therefore, the snippet bellow abuse somewhat of closures to come up with a Class Decorator that fills the needs of the O.P. : a class variable that remains “unified” for reading and writing within derived classes.

    Moreover, as a bonus, I wrap the attribute in a descriptor which makes the attribute unchangeable within instances as well – so whenever the attribute is written – in either a subclass or instance of a subclass of the original class, the class attribute is properly updated.

    As the Zen of Python puts it: “If the implementation is hard to explain, it is a bad idea” – I don’t think I could come with anything harder — we are talking of scoped dynamically generated meta-classes here. It will work, but it looses this is “unpythonic” code as it is very cryptic due to the heavy use of the class, metaclass, closures and descriptor mechanisms.

    def SingletonAttrs(**names):
        keys = names.keys()
        def class_decorator(cls):
            class Meta(type):
                def __getattribute__(cls, attr):
                    if attr in keys:
                        return type.__getattribute__(owner_cls,  attr)
                    return type.__getattribute__(cls, attr)
                def __setattr__(cls, attr, value):
                    if attr in keys:
                        class Wrapper(object):
                            def __init__(self, value):
                                self.__set__(None, value)
                            __set__ = lambda self, instance, value: setattr(owner_cls,"__" +  attr, value)
                            __get__ = lambda self, instance, owner: type.__getattribute__(owner_cls, "__" + attr)
                        return type.__setattr__(owner_cls,  attr, Wrapper(value))
                    return type.__setattr__(cls, attr, value)
            owner_cls = Meta(cls.__name__, cls.__bases__, cls.__dict__.copy())
            for key in keys:
                setattr(owner_cls, key, names[key])
            return owner_cls
        return class_decorator
    
    if __name__ == "__main__":
    
        @SingletonAttrs(a="value 1", b="value 2")
        class Test(object):
            pass
    
        class TestB(Test):
            pass
    
        t = Test()
        print t.a
        print t.b
        tb = TestB()
        tb.a = "value 3"
        print Test.a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm about to embark on some large Python-based App Engine projects, and I think
I am creating a sql database which logs calls from companies (or separate departments
First things first, I am a Python beginner, with a typical C++/Java background for
Working on importing a tab-delimited file over HTTP in Python. Before inserting a row's
I have a JSON object that I am trying to read using Python but
I'm heading into hour 5 or so of my experience with Python, and so
I'm currently trying to wrap my head around pointers in C, coming from front-end
I recently endeavoured to learn about multiple threading, and ran into the following unexpected
I just wrote a Flex app which handles some Wikipedia text content as strings.
Staying away from javascript I endeavor to use Check_Clicked event handler to populate my

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.