1

Lets say I have a function which I have defined:

def command("filename", function, error):

For 'function' I can input one of the following two functions:

def linear:
    return x

def exponential:
    return e**x

I want to save the figure under a name depended on the function used. So the name of figure will be something like this: filename_function.jpg

The command for this is plt.savefig(filename_+????+'.png')

How do I input the name of the specific function used into saved file?

So if my command is command("hello.txt", linear, error), then my save file should read hello_linear.png.

Update: Thanks everyone. I am new to Python, and I guess I caught myself tied up in nonsense approaches to something that can be implemented very simply. The answer is function.__name__ exactly as written.

2 Answers2

1

Functions are objects and have attributes: Simply use function.__name__.

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • 1
    If I understand the question, OP wants to know how to access `function` from within that very function. – shx2 Jun 06 '14 at 20:49
1

Using the magical inspect module:

def foo():
    return inspect.currentframe().f_code.co_name

foo()
=> 'foo'
shx2
  • 61,779
  • 13
  • 130
  • 153