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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:44:04+00:00 2026-05-14T03:44:04+00:00

Apologies if I’ve got the terminology wrong here—I can’t think what this particular idiom

  • 0

Apologies if I’ve got the terminology wrong here—I can’t think what this particular idiom would be called.

I’ve been trying to create a Python 3 class that statically declares instances of itself inside itself—sort of like an enum would work. Here’s a simplified version of the code I wrote:

class Test:
    A = Test("A")
    B = Test("B")

    def __init__(self, value):
        self.value = value

    def __str__(self):
       return "Test: " + self.value

print(str(Test.A))
print(str(Test.B))

Writing this, I got an exception on line 2 (A = Test("A")). I assume line 3 would also error if it had made it that far. Using __class__ instead of Test gives the same error.

  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in Test
NameError: name 'Test' is not defined

Is there any way to refer to the current class in a static context in Python? I could declare these particular variables outside the class or in a separate class, but for clarity’s sake, I’d rather not if I can help it.

To better demonstrate what I’m trying to do, here’s the same example in Java:

public class Test {
    private static final Test A = new Test("A");
    private static final Test B = new Test("B");

    private final String value;

    public Test(String value) {
        this.value = value;
    }

    public String toString() {
        return "Test: " + value;
    }

    public static void main(String[] args) {
        System.out.println(A);
        System.out.println(B);
    }
}

This works as you would expect: it prints:

Test: A
Test: B

How can I do the same thing in Python?

  • 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-14T03:44:04+00:00Added an answer on May 14, 2026 at 3:44 am

    After you defined the class, just add these two lines:

    Test.A = Test("A")
    Test.B = Test("B")
    

    A class in Python is an object like any other and you can add new variables at any time. You just can’t do it inside the class since it’s not defined at that time (it will be added to the symbol table only after the whole code for the class has been parsed correctly).

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

Sidebar

Related Questions

Apologies if this question has already been asked but I do not think I
Apologies if this is too ignorant a question or has been asked before. A
Apologies if this question is a bit obscure, I've been banging my head against
Apologies if this is a dumb question. In previous ASP.NET projects, I've been using
Apologies if this has been answered previously. Say I have the following table: colA
Apologies if this is classic ASP 101, but its been so long since I
Apologies if this has been answered before (I couldn't find the answer when I
Apologies if this is trivial. I was trying to run this code - http://oreilly.com/pub/h/974#code
Apologies for the horrible title. I spent 10 minutes trying to explain this in
Apologies if this has been covered thoroughly in the past - I've seen some

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.