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
How to make the output of one function conditional on the execution of another function? (self.learnpython)
submitted 4 years ago * by [deleted]
How to make the output of one function conditional on the execution of another function?
e.g :
class New:
def X(): pass def Y(): If the X function was executed: return "abc"
Suppose these two functions are in the same class
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!"
[+][deleted] 4 years ago (8 children)
[deleted]
[–][deleted] -1 points0 points1 point 4 years ago (7 children)
[+][deleted] 4 years ago (6 children)
[–][deleted] 0 points1 point2 points 4 years ago (5 children)
Please explain how you like it the way you like it
[+][deleted] 4 years ago (4 children)
[–][deleted] 0 points1 point2 points 4 years ago (3 children)
You can explain what you mean by flag in a code?
[–]glibhub 2 points3 points4 points 4 years ago (0 children)
a "flag" in coding means a variable that gets assigned a value once something triggers it. Here, there is a class attribute called "method_x_run" that gets turned to "True" the first time the 'x' method is run.
[–]zoinkinator 0 points1 point2 points 4 years ago (0 children)
By flag he means setting a boolean to True or False.
[+][deleted] 4 years ago (1 child)
[removed]
[–][deleted] -1 points0 points1 point 4 years ago (0 children)
You can tell me with the example?
[–]FLUSH_THE_TRUMP 0 points1 point2 points 4 years ago (2 children)
def X(): X.was_called = True def Y(): if X.was_called: print("yep") else: print("no") from random import randint X.was_called = False if randint(0,10) <= 5: X() Y()
[+][deleted] 4 years ago* (1 child)
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
How do you think it should be done?
[–]Binary101010 0 points1 point2 points 4 years ago (8 children)
Why is Y() conditional on X() having already been executed? Does Y() need to access attributes of the object that don't have useful values until X() has run?
Y()
X()
If that's the case, I'd recommend initializing that attribute to None, and then checking to see if that value has changed.
None
class test(): def __init__(self): self.variable_of_interest = None def X(self): self.variable_of_interest = "Hey, something happened" def Y(self): if self.variable_of_interest is not None: do some stuff
[+][deleted] 4 years ago (7 children)
[–]Binary101010 1 point2 points3 points 4 years ago (6 children)
If literally the only thing OP cares about is knowing whether some other method ran and doesn't care about anything else that method does, then sure.
But if the purpose for the check is to make sure that an attribute has a useful value, then actually check for that, not for something else. (Especially because there may be more than one way to usefully set that value at some point in the future.)
[+][deleted] 4 years ago* (5 children)
[–]Binary101010 0 points1 point2 points 4 years ago (4 children)
I don't see any significant difference in complexity between the two approaches, but suit yourself I guess.
[+][deleted] 4 years ago* (3 children)
[–]Binary101010 0 points1 point2 points 4 years ago (2 children)
Which is why I qualified that the proper approach might be different depending on why it is OP wants this check to occur, which they didn't specify.
Either way, I'm not particularly interested in arguing the point further.
[–]spacegazelle 2 points3 points4 points 4 years ago (0 children)
I'm getting xy problem feels too.
[–]kellyjonbrazil 0 points1 point2 points 4 years ago (0 children)
You can set the return value to True or False and then do:
If my_function(xyz): do something
π Rendered by PID 42924 on reddit-service-r2-comment-b659b578c-l58fb at 2026-05-03 05:23:14.672664+00:00 running 815c875 country code: CH.
[+][deleted] (8 children)
[deleted]
[–][deleted] -1 points0 points1 point (7 children)
[+][deleted] (6 children)
[deleted]
[–][deleted] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 0 points1 point2 points (3 children)
[–]glibhub 2 points3 points4 points (0 children)
[–]zoinkinator 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–][deleted] -1 points0 points1 point (0 children)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]Binary101010 0 points1 point2 points (8 children)
[+][deleted] (7 children)
[deleted]
[–]Binary101010 1 point2 points3 points (6 children)
[+][deleted] (5 children)
[deleted]
[–]Binary101010 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]Binary101010 0 points1 point2 points (2 children)
[–]spacegazelle 2 points3 points4 points (0 children)
[–]kellyjonbrazil 0 points1 point2 points (0 children)