1

I need to get the name of a variable in python. For example:

task = EmailTask()

I need to get the "task" mapped to the actual object.

The use case for this is as follows: I am building an ETL tool which will have a lot of reusable generic tasks and instead of a user providing an extra task name for every instance he creates, I will use the variable name that the user has chosen. This variable name is only used for logging purposes. Example of use case:

process_task = MoveDataTask()
email_task = EmailTask()

job = Job(
    tasks: [process_task, email_task])

job.start()

I will have to use globals() since the task will be declared in the parent namespace. I understand that I will have a place a few constraints on how the user declares the whole "job", in that, he will have to assign each task to a variable name, instead of doing the following:

job = Job(
    tasks: [MoveDataTask(), EmailTask()])

I know that globals() pretty much gives me what I want, but I have reservations about using it since it a little bit non-standard usage of python. Also as I understand, there can be multiple names for the same object in globals(), so, therefore, I need to somehow get the parent name ("process_task", "email_task" from above's an example).

Are there any other pitfalls/issues/things I am missing with this approach?

  • 2
    Why not save yourself all the magic, and use a string name in the constructor? `MoveDataTask("process_task")` – Ned Batchelder Jul 06 '19 at 01:57
  • I am trying to make this as easy and as quick as possible to build a new job. A good name is already chosen by the variable assigned to the task, I was thinking I would use that already. – bronzefalcon Jul 06 '19 at 03:04
  • IMO, this is just not the way things are done. Variable names shouldn't be meaningful in this way. Even if you could do this, if someone else ever needed to understand your code, they'd be confused by this. - you should add a `getName()` method to each task so that each task object knows its own name and can return it. If the names don't match the objects one-one, then you should do what @NedBatchelder suggests and pass the name into the object when you create it. A `getName() `method on the object would then return that. This is a very standard answer to such a problem. – CryptoFool Jul 06 '19 at 03:14

1 Answers1

0

Create helper functions (probably in a dedicated module for the sake of a namespace) and pass them to the job:

def process_task():
    return MoveDataTask()

def email_task():
    return EmailTask()

# ...
job.add_task(process_task)
job.add_task(email_task)

# ...
for task in job.tasks:
    task()
ipaleka
  • 3,745
  • 2
  • 13
  • 33
  • The only reason I am going down this path is to make it as quick and easy (with as less code as possible) to have the user create a new "job". You approach does not work because of the too much boilerplate code. – bronzefalcon Jul 06 '19 at 03:07
  • I don't see that this answer addresses the OPs question at all. This does not provide printable names for his tasks, which is I believe what he is asking for. – CryptoFool Jul 06 '19 at 03:18
  • It's your project and you know the best. If you had named the thing Report instead of Job then it would have probably been less confusion. EDIT: I'm out, there's job.start() line at the end of code that fooled me. – ipaleka Jul 06 '19 at 03:28