Python Forum
Printing the variable from defined function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing the variable from defined function
#5
(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)
Output:
a = 3
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.

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.
Reply


Messages In This Thread
RE: Printing the variable from defined function - by jws - Sep-02-2023, 08:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 392 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable for the value element in the index function?? Learner1 8 747 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 660 Nov-23-2023, 02:53 PM
Last Post: rob101
  Function parameter not writing to variable Karp 5 1,045 Aug-07-2023, 05:58 PM
Last Post: Karp
  Getting NameError for a function that is defined JonWayn 2 1,166 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 3,022 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,371 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,092 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  User-defined function to reset variables? Mark17 3 1,717 May-25-2022, 07:22 PM
Last Post: Gribouillis
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,243 Apr-05-2022, 04:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020