-3

I have a class created for an object in Python. I want to create an instance with name given by the user.

class task:

    def __init__(self, task , urgency):
        self.task = task
        self.urgency = urgency

I have a function which creates instance of the class "task" named 'create()', it needs 3 attributes name , task , urgency level. I want to convert name which is a string to a variable to store the task instance. Further I could use the name to change its property or use it somewhere else

  • 1
    shouldn't it be `class task:`? And have you thought of using a dictionary to achieve this? – Aravind Suresh Thakidayil Sep 29 '20 at 11:04
  • 1
    You do *not* want to use a user input value as the name of a variable. Believe me, you *don't.* You either want to make the "name" another attribute of the task object, or you want a dict of tasks and use that "name" as a key in the dict. – deceze Sep 29 '20 at 11:05

1 Answers1

-1

I think it’s more efficient to either use a dictionary of user input values depending upon your use case:

// place user obj as dynamic var at top of page, this could be a function that returns this obj from user 

user_obj = {
    “task”: str,
    “urgency”: int,
}

// and inside the rest of your functions you can simply do:

user_obj[“task”] = “whatever input var here”
joe hoeller
  • 1,248
  • 12
  • 22
  • Sorry I misspoke I meant it could be either json or a python dictionary. I rewrote it to communicate better. Thanks for pointing that out. – joe hoeller Sep 29 '20 at 11:25
  • I dont know if its a web ui user vs some automation script etc; please dont exercise "developer pendantry". – joe hoeller Sep 29 '20 at 15:47