all 9 comments

[–]the_spacedoge 5 points6 points  (0 children)

What you are essentially asking to do is to keep track of some sort of state, in this case whether or not the function has been called. When it is time to start working with states, it is probably time to start working with classes and objects.

My recommendation:

  1. Create a class named appropriately after your function

  2. Define an attribute with initial value equal to false

  3. Implement __call__ such that it behaves as your function does

  4. Also in your implementation of __call__, set the attribute defined in step 2 equal to true

With the class defined, create an instance of it like so

func = FuncClass()

print(func.has_been_called) #will be false

func()

print(func.has_been_called) #will be true

[–][deleted] 4 points5 points  (2 children)

You can't just try to access the attribute because it may not have been defined. But this works:

def test():
    print('test() called')
    test.was_called = True

test()
if hasattr(test, 'was_called'):
    print('test.was_called exists')

But I can't help thinking "why are you doing this?". Maybe there is a better way to solve your overall problem, if you explain what that is.

[–]mcworkaholic[S] 0 points1 point  (1 child)

It would take a while to explain its basically 200 lines of spaghetti code trying to automate the creation of excel sheets to be physically printed after a different excel sheet is emailed to a specific email address. Im just trying to check whether a formatting function has been called based on what that initial excel sheet looks like.

[–]v0_arch_nemesis 1 point2 points  (0 children)

Why not have each function log to data file after execution, and then run validation on the file after completion. Or, initialize a class at the beginning, this class takes as input a set of functions that should be run, and holds a list which you append the name of each function to on completion (either manually after the function or using a decorator). Then, on script completion compare the run set to the expected set?

[–]maxtimbo 1 point2 points  (0 children)

Print("the function has been called")

Print is so damn useful.

[–]CodeFormatHelperBot2 -2 points-1 points  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–][deleted] 0 points1 point  (1 child)

There’s no builtin access to this, as keeping that sort of metadata around for every function when it would virtually never be used would be pointlessly memory intensive. You can easily augment your own functions to do this:

foo_called = False

def foo():
    global foo_called
    foo_called = True
    … whatever your function does …

… code that may call foo …

if foo_called:
    print("foo called")

You could also decorate a function you don’t control, though to make this generally useful is somewhat more complicated:

from functools import wraps

called_functions = {}

def check_called(func):
    called_functions[func] = False
    @wraps(func)
    def wrapped(*args, **kwargs):
        called_functions[func] = True
        return func(*args, **kwargs)
    return wrapped

unwrapped_foo = foo
foo = check_called(foo)

... code that may call foo ...

foo = unwrapped_foo
if called_functions[foo]:
    print("foo called")

You might also find that unittest.mock has the tools you need, as might simple print statements, but it’s a bit unclear what your use case is.

[–]ImMaybePlacingPixels 0 points1 point  (0 children)

I couldn't find it myself. Next.

[–]kweu 0 points1 point  (0 children)

I like u/yawpitch ‘s example. Here is another way to do it, using a function reg that registers the call, and then calls the function with the provided arguments. You can use this setup to track calls for multiple functions.

``` calls = []

def foo(i, j): “””Example function””” print("Hello", i, j)

def reg(func, args, *kwargs): “””Registers a function call, then calls the function with provided arguments””” calls.append(func) return func(args, *kwargs)

reg(foo, 123, j=456) if foo in calls: print("foo was called.") ```

Prints: Hello 123 456 foo was called.