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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:31:35+00:00 2026-05-25T12:31:35+00:00

In Python, how do I get a reference to the current class object within

  • 0

In Python, how do I get a reference to the current class object within a class statement? Example:

def setup_class_members(cls, prefix):
    setattr(cls, prefix+"_var1", "hello")
    setattr(cls, prefix+"_var2", "goodbye")

class myclass(object):
    setup_class_members(cls, "coffee")  # How to get "cls"?

    def mytest(self):
        print(self.coffee_var1)
        print(self.coffee_var2)

x = myclass()
x.mytest()

>>> hello
>>> goodbye

Alternatives that I’ve written off are:

  1. Use locals(): This gives a dict in a class statement that can be written to. This seems to work for classes, however the documentation tells you not to do this. (I might be tempted to go with this alternative if someone can assure me that this will continue to work for some time.)

  2. Add members to the class object after the class statement: My actual application is to derive a PyQt4 QWidget class with dynamically created pyqtProperty class attributes. QWidget is unusual in that it has a custom metaclass. Very roughly, the metaclass compiles a list of pyqtProperties and stores it as additional member. For this reason, properties that are added to the class after creation have no effect. An example to clear this up:

from PyQt4 import QtCore, QtGui


# works
class MyWidget1(QtGui.QWidget):
    myproperty = QtCore.pyqtProperty(int)


# doesn't work because QWidget's metaclass doesn't get to "compile" myproperty
class MyWidget2(QtGui.QWidget):
    pass
MyWidget2.myproperty = QtCore.pyqtProperty(int)

Please note that the above will work for most programming cases; my case just happens to be one of those unusual corner cases.

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

    AFAIK there is two way to do what you want:

    1. Using metaclass, this will create your two variables in class creation time (which i think is what you want):

      class Meta(type):
          def __new__(mcs, name, bases, attr):
              prefix = attr.get("prefix")
              if prefix:
                  attr[prefix+"_var1"] = "hello"
                  attr[prefix+"_var2"] = "goodbye"
      
              return type.__new__(mcs, name, bases, attr)
      
      class myclass(object):
          __metaclass__ = Meta
          prefix = "coffee"
      
          def mytest(self):
              print(self.coffee_var1)
              print(self.coffee_var2)
      
    2. Create your two class variable in instantiation time:

      class myclass(object):
      prefix = “coffee”

       def __init__(self):     
           setattr(self.__class__, self.prefix+"_var1", "hello")
           setattr(self.__class__, self.prefix+"_var2", "goodbye")
      
       def mytest(self):
           print(self.coffee_var1)
           print(self.coffee_var2)
      

    N.B: I’m not sure what you want to achieve because if you want to create dynamic variables depending on the prefix variable why are you accessing like you do in your mytest method ?! i hope it was just an example.

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

Sidebar

Related Questions

When you call the object.__repr__() method in Python you get something like this back:
How can I iterate over a string in Python (get each character from the
Is there any way to get Python to use my ActiveTcl installation instead of
I can't seem to get Python to import a module in a subfolder. I
Is there a built-in method in Python to get an array of all a
How do I get my Python program to sleep for 50 milliseconds?
I'm a long time C++/Java developer trying to get into Python and am looking
In python is it possible to get or set a logical directory (as opposed
In Python specifically, how do variables get shared between threads? Although I have used
In python, if I say print 'h' I get the letter h and a

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.