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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:20:26+00:00 2026-05-27T12:20:26+00:00

I’d like to store some information about a class as class (static) variables. However,

  • 0

I’d like to store some information about a class as class (static) variables. However, I can’t figure out how these things get initialized. Here is a basic, dumb example:

class A(object):
    clsVar = 'a'
    @classmethod
    def clsMeth(cls):
        print 'changing clsVar'
        cls.clsVar = 'b'
    A.clsMeth()
# prints 'changing clsVar'

print A.clsVar    # prints 'a'
A.clsVar = 'b'
print A.clsVar    # prints 'b'

Since the function got called (as the print statement worked), why didn’t the class variable stay changed? Do I have to use a metaclass if I don’t want to do it after the class definition completes?

[Specifically, I want clsMeth to be a decorator and have the class variable be a list of all the functions that were so decorated. I’m guessing this isn’t the right way to go about accomplishing that, so I’ve moved on, but I’m still curious.]

EDIT: As numerous people have pointed out, the code above won’t run. I was running it in an IPython session where the call to A.clsMeth() would refer to a previous version of A and run. Such are the risks of using an interpreted language, I guess. I ended up going with something like this:

outsideDict = {}
def outsideDec(func):
    outsideDict[func.__name__] = func

class A(object):
    @outsideDec
    def someMethod(self):
        print 'ID %s' % id(self)

    def otherMethod(self):
        print 'other'

print outsideDict
one, two = A(), A()
outsideDict['someMethod'](one)
outsideDict['someMethod'](two)

Perhaps this should be another question, but when outsideDec gets run, is there a way to tell what class it’s argument is a member of? Or is there a better way of doing introspection like this in Python? I recognize I’m veering off course here so I’ll accept the answer below and do more research. Thanks everyone!

  • 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-27T12:20:27+00:00Added an answer on May 27, 2026 at 12:20 pm

    The call to A.clsMeth() in the definition of A will not run, as A does not exist at that point:

    >>> class A(object):
    ...     clsVar = 'a'
    ...     @classmethod
    ...     def clsMeth(cls):
    ...         print 'changing clsVar'
    ...         cls.clsVar = 'b'
    ...     A.clsMeth()
    ... 
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "<stdin>", line 7, in A
    NameError: name 'A' is not defined
    

    The code may have seemed to work if A had been defined previously (eg, if you were testing it out in the REPL), but the call to A.clsMeth would have been called on the old class, which would be shadowed by the new one.

    However, we can definitely put that call after the definition and get the result you want:

    >>> class A(object):
    ...     clsVar = 'a'
    ...     @classmethod
    ...     def clsMeth(cls):
    ...         print 'changing clsVar'
    ...         cls.clsVar = 'b'
    ...
    >>> A.clsMeth()
    changing clsVar
    >>> A.clsVar
    'b'
    

    Of course, as fabianhrj noted, you can put it in the constructor as well, but it won’t be called until you create an instance.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.