0

Let's say that I have an import statement like this:

from foo import foo_func as myfunc

Is there a practical and clean way to get the alias name for foo_func as a string?

I am aware of the name property of a function but if I were do do this: print(myfunc.__name__) I would only get 'foo_func' and not 'myfunc'

Is there something similar to let me get the alias name itself.

Mad Max
  • 535
  • 4
  • 8
  • No, this is fundamentally impossible. When you pass a parameter to a function, the *only* thing the function gets is the object itself, not any information about the name by which it was referenced. – jasonharper May 29 '23 at 02:03
  • Variable names are code, not data. You shouldn't *care* what the name of any variable (there could be many) referring to the object may be. If such a name *is* important, you don't want to pass just a reference to the function; you want something like a tuple that pairs the function reference with an explicit name. – chepner Jun 16 '23 at 13:57
  • At the time I posed this question, I was under the mistaken belief that import aliases were treated as some kind of special construct. However, it appears that they are treated as simply variables. If that is the case then the responses I have received make sense. – Mad Max Jun 17 '23 at 14:11

0 Answers0