How can I get the name of an object?

Objects do not necessarily have names in Python, so you can’t get the name.

When you create a variable, like the x, y, z above then those names just act as “pointers” or “references” to the objects. The object itself does not know what name(s) you are using for it, and you can not easily (if at all) get the names of all references to that object.

However, it’s not unusual for objects to have a __name__ attribute. Functions do have a __name__ (unless they are lambdas), so we can build fun_dict by doing e.g.

fun_dict = {t.__name__: t for t in fun_list)

Leave a Comment

tech