Sep-02-2023, 08:47 PM
(Sep-02-2023, 07:24 PM)deanhystad Wrote: Avoid using the names of built-in functions (input) or classes as variable names. Here it does no harm, but it is a bad habit and it easily leads to confusion. Or maybe it already has?
I'm having trouble understanding your question. The code does exactly what I would expect, so looking at the code doesn't help much. Which of the prints doesn't work as you expect? The one that prints "3", the value of "a", or the one that prints "input", the first string returned by split("="). Maybe you should post what you want for output.
Do you want your function to print the letter "a" because it is the variable passed to the function? If so, why? Like so many things, this is possible, but of what use is it? Here is some code that retrieves the python expression that called function, and extracts the argument variable name from that line.
import traceback import re a = 3 def function (arg): stack = traceback.extract_stack() print(re.search("\((.*)\)", stack[-2].line).group(1), "=", arg) function(a)That was fun to write, but I cannot see any use for such a thing. It certainly would be of no use in a filename generator function.
Output:a = 3
Sorry, yes, the context for this would be so that I can generate plots and name them based on the name of the dataframe used to create- here, "a" would be the df containing the information. So I would be outputting a file "time_plot_a.png" for example.