4

For training, I want to atumaticly create users in a system using class.

Here is my code:

class CsrUser(object):
    def __init__(self, f_name, l_name, login, role, sex, password ='Hot12345'):
        self.f_name = f_name
        self.l_name = l_name
        self.login = login
        self.role = role
        self.sex = sex
        self.password = password

I want to create dynamic objects of the class from the user input,

#i want to create something like this
def get_users_data():
    new_user = input("Enter the user name")
    ... #get all the data
    new_user = CsrUser(...)

I want the name of the object to be the value that inside new_user

8
  • With the name of the object, do you mean the name of the variable referencing the object? Commented Sep 30, 2013 at 15:38
  • 1
    what is in a local variable name ? As long as it is readable you get the desired output ? Commented Sep 30, 2013 at 15:39
  • I think so, i am kind of new with all the class thing. if i create new = Class(). So i want the new to change to the name that i get from the user. Commented Sep 30, 2013 at 15:45
  • @karthikr I don't know how many users will be create so i don't want to hard code 20 Classes names. I am sure i think that way only because my lack of experience Commented Sep 30, 2013 at 15:47
  • Any variable name is fine, as long as it refers to the right object you are good to go. Dont worry about changing the object variable name Commented Sep 30, 2013 at 15:48

1 Answer 1

6

I don't think that naming a variable (object in your case) using a variable is a good idea. If you want to keep track of all of your users by name, you should use a dictionary instead:

#make dictionary
user_data = {}

#make a new user object
new_user = CsrUser(...)

#insert your new user object into the dictionary
#use new_user.f_name or new_user.l_name here in place of new_user.name (or combine both)
user_data[new_user.name] = new_user  

#to get a user object out of the dictionary 
a_user = user_data["name_here"]
Sign up to request clarification or add additional context in comments.

4 Comments

I want to create 20 users with my class and track which one is what. This is why i though about making a dynamic name
A dictionary is exactly what you should be using. I took a look at your profile and your bio says you're studying python, you should get used to using dictionaries.
I guess i need to study them more. i just don't fully understand the class concept and how to combine them with dictionary. I will try your solution and to understand your answer and after that i will come back. thanks :-)
Putting objects in a dictionary is no different than putting integers or strings in a dictionary. A dictionary works by having key/value pairs. Read more here : tutorialspoint.com/python/python_dictionary.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.