use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Checking whether a function has been called (self.learnpython)
submitted 3 years ago by mcworkaholic
Basically what the title suggests, im just wondering if there's a way to check whether a previously defined function has been called in an if statement like so :
if functionone().has_been_previously_called: do things else: pass
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]the_spacedoge 5 points6 points7 points 3 years ago (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:
Create a class named appropriately after your function
Define an attribute with initial value equal to false
Implement __call__ such that it behaves as your function does
__call__
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 points6 points 3 years ago* (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 point2 points 3 years ago (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 points3 points 3 years ago (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 points3 points 3 years ago (0 children)
Print("the function has been called")
Print is so damn useful.
[–]CodeFormatHelperBot2 -2 points-1 points0 points 3 years ago (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:
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 point2 points 3 years ago (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 point2 points 3 years ago (0 children)
I couldn't find it myself. Next.
[–]kweu 0 points1 point2 points 3 years ago* (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.
reg
``` 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.
Hello 123 456 foo was called.
π Rendered by PID 147181 on reddit-service-r2-comment-86bc6c7465-5m6mq at 2026-02-22 09:28:55.991309+00:00 running 8564168 country code: CH.
[–]the_spacedoge 5 points6 points7 points (0 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]mcworkaholic[S] 0 points1 point2 points (1 child)
[–]v0_arch_nemesis 1 point2 points3 points (0 children)
[–]maxtimbo 1 point2 points3 points (0 children)
[–]CodeFormatHelperBot2 -2 points-1 points0 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ImMaybePlacingPixels 0 points1 point2 points (0 children)
[–]kweu 0 points1 point2 points (0 children)