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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:44:00+00:00 2026-06-08T23:44:00+00:00

Is there a way in Python, that i can use the variable value for

  • 0

Is there a way in Python, that i can use the variable value for creation of Class instance

class Test(object):
    def __init__(self, object):
        self.name = object
        print "Created Object :",self.name

a1 = Test('a1')
print a1.name
b2 = 'a2'
b2 = Test(b2)
print a2.name

In this example, i want the class instance name should be ‘a1’ and ‘a2’ respectively. I cannot directly use the value ‘a2’ as it is computed from some other process, but the class instance name must match it such that it can be accessed.

In the above example, it gives error :

 Created Object : a1
 a1
 Created Object : a2

 Traceback (most recent call last):
 File "D:\a\h", line 12, in <module>
 print a2.name
 NameError: name 'a2' is not defined
  • 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-06-08T23:44:01+00:00Added an answer on June 8, 2026 at 11:44 pm

    The closest thing to what you are looking for is to store all your instances in a dictionary:

    class Test(object):
        def __init__(self, n):
            self.name = n
            print "Created Object :",self.name
    
    d = {}
    d['a1'] = Test('a1')
    print d['a1'].name
    b2 = 'a2'
    d[b2] = Test(b2)
    print d['a2'].name
    

    There is no connection between the name of a variable that references an object and the object itself.

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

Sidebar

Related Questions

Is there a way to transfer a new class instance (python class that inherits
Is there no magic python way of accessing the instance of the class that
Is there a Python module that can be used in the same way as
Is there a way that I can add alias to python for encoding. There
class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: self.value.__add__(100) self.run() def run(self):
Is there a way that when a user types python setup.py install to install
Is there a way in Python to list all the currently in-use drive letters
Is there an easy way to use python string formatting from within a django
I have a question about righteous way of programming in Python... Maybe there can
Is there a way in C# that I can signal to VS to force

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.