-1

Suppose I had this class

class employee(object)
    def __init__(self,name):
        return self.name
    def something(self):
        pass
    ...

Leading to

if __name__== "__main__":
    name = input("what is your name ")
    user1 = employee(name)
    user1.something()
    ...

I want the user1 instance to be the name inputted by the user so that I can have unique instances. How do I go about adding instances based on user input in the main section?

so if I run the program and inputted "tim", the outcome I would want is:

tim.name = "tim"
....

UPDATE

Seems like the above is unclear, let me try to explain using my actual code:

So I have this Spotify API:

class Spotify(object):
def __init__(self,user):
    self.client_id = ''
    self.client_secret = ''


def client_credentials(self):
     pass
def get_header_token(self):
     pass
...

In the end,

if __name__== "__main__":
        
        user = input("Enter username ")
        user = Spotify(user)
        user.request_author()
        ...

I am trying to get the user variable to the input the user provides, such as if the user inputted "tim123", the user variable would also be tim123.

So I could perform:

tim123.name

Think my mind is going completely blank and there should be an easy solution for this. I am sure this is very unpractical but I don't know how I would do this in case I ever needed to.

Meruem
  • 33
  • 4
  • Do you want the variable name used to access the class to be determined by user input? – aleph-null Jul 27 '20 at 22:42
  • Yepp. I will probably add a unique id somehow later but I'm just generally curious how variable names can be created by user input, if it can? – Meruem Jul 27 '20 at 22:45
  • [This](https://stackoverflow.com/questions/29824194/setting-user-input-to-variable-name) might help, although it's not too useful. A better solution might be using a while loop to take input, and using those inputs for your names. – aleph-null Jul 27 '20 at 22:48

2 Answers2

2

Change

return self.name

to

self.name = name

if name== "main":

variable_name = raw_input("Enter variable name:") # User.     
enters "tim123"

name = input("Enter Name")

globals()[variable_name] = employee(name)

tim123.name
AFzal Saiyed
  • 119
  • 5
  • But then I would have to manually code the variable names (user1) in the main section. Was hoping to have the user1 variable be set by the input of the user, if it makes sense. – Meruem Jul 27 '20 at 22:48
  • sorry i am on mobile and cannot type properly. – AFzal Saiyed Jul 27 '20 at 23:12
1

Based on your comment, it sounds like you are looking for exec() or eval(). Link. My solution would be to do something like:

class employee(object):
    def __init__(self,name):
        self.name = name
name = input("what is your name ")

exec(f"{name} = employee('{name}')")

(and then you would access joe.name, if the user inputted joe, or bob.name, if the user inputted bob, etc.). Alternatively, you could use locals() or globals()

Hope this helped!

a1426
  • 256
  • 2
  • 8
  • Thanks I will have a look! I made update to my question but I think this is what i am looking for! – Meruem Jul 27 '20 at 23:04