0

I am making a discord bot and want to create new instances of a class using user input, so essentially something like this:

class myclass:
  def __init__(self):
    pass
x = input(“”)
x = myclass()

Of course this doesn’t work and instead just name the class instance x. Is there any way to name the class instance what was inputted?

The contents of the class don’t matter, I am just interested in the name.

  • 1
    Can I talk you out of this? It seems like a really bad idea. Provided it could be done by some arcane tweak, wouldn't you want to have a variable name that you decide, instead of a variable name that changes to an arbitrary value at runtime? Perhaps what you want can be better accomplished in another way? – Roberto Jan 13 '21 at 01:38
  • ...e.g. you could set that "name" as an instance attribute? – Roberto Jan 13 '21 at 01:41
  • 1
    So if the user entered `myclass` as the name for the instance, you'd be perfectly OK with it overwriting the class definition, and making it impossible to create any further instances? – jasonharper Jan 13 '21 at 01:49
  • My end goal here is to have a user using my bot name a subreddit and the bot will gather 150 posts from the sub and these can be accessed by further interaction with the bot as well as replaced/refreshed without user input after they have been used. I figured how to do all of that with a class I made but if I made blank classes it would place a hard limit on how many subreddits I could connect. This is the best way I could think of, though I am open to other solutions. – Demitrius Urban Jan 13 '21 at 02:08
  • Thanks wjandrea, I think variable variables will work. – Demitrius Urban Jan 13 '21 at 13:55

0 Answers0