2
def method(x, y, z):
   def nested_method(a, b, c):
      if a + b + c < 7:
         for i in range(0, 7):
            if i == a + b +c
               return i
   nested_method(3, 4, 0)
   nested_method(3, 0, 1)
   nested_method(1, 2, 0)

returns in method: None, 4, 3

If nested method returns something i want the method to return it. When i only need one answer i firtsly check the more prioritiesed.

first priority: 3, 4 and 0

second priority: 3, 0 and 1

...

so it would return 4

6
  • Do you mean if nested_method returns True? Functions without a return statement return None. Commented Jun 7, 2022 at 17:34
  • Can you provide an example of why you would need to this? I would like to help, I just can't imagine a situation this would be best practice. Commented Jun 7, 2022 at 17:34
  • @JacquesGaudin i want the method to return a soon as one of the called nested_methods returns anything, because i call the nested_methods with higher priorities earlier then the one with lower Commented Jun 7, 2022 at 17:36
  • i need this for my tictactoe bot who firstly checks if he can win, then if the player can win, then if he could drop next to where he already dropped Commented Jun 7, 2022 at 17:40
  • if you want to return something from method you need to have a return statement in method... Commented Jun 7, 2022 at 18:35

3 Answers 3

1

This would return the first comparison that returns True

def method(a, b, c):
   def nested_method(d, e):
      if d == e:
         return True
   if nested_method(a, b):
      return nested_method(a, b)
   if nested_method(a, c):
      return nested_method(a, c)
   if nested_method(b, c):
      return nested_method(b, c)

You may want to add a printing statement to know which one is the one working...

This one would work in case that the three values are the same:

def method2(a, b, c):
   def nested_method(d, e):
      if d == e:
         return True
   
   comparisons = {'ab': nested_method(a, b), 'ac': nested_method(a, c), 'bc': nested_method(b, c)}

   return [comparisons[comp] for comp in comparisons if comparisons[comp] == True]
Sign up to request clarification or add additional context in comments.

8 Comments

You might better use the := operator to avoid repeatedly invoking nested_method. But nested_method can only return True or None.
is it possible so that the nested method returns method with its value without checking in outer scope
Not sure if I understand: Do you want the nested function to return the function that defines it?
@Ignatius Reilly yes
@Ignatius Reilly i think we missunderstand each other. i want that nested function returns 'return value' or something like that, so that if it returns the method function returns value
|
1

Perhaps what you're looking for is:

def method(a, b, c):
   def nested_method(d, e):
      if d == e:
         return True
   if nested_method(a, b) is not None: return True
   if nested_method(a, c) is not None: return True
   if nested_method(b, c) is not None: return True

If you wish to know which pair match:

def method(a, b, c):
   def nested_method(d, e):
      if d == e:
         return True
   if nested_method(a, b) is not None: return ('a', 'b')
   if nested_method(a, c) is not None: return ('a', 'c')
   if nested_method(b, c) is not None: return ('b', 'c')

Given the effect return has upon control flow, this works, but you could also write the following, using a conditional expression rather than statement.

def method(a, b, c):
   def nested_method(d, e):
      if d == e:
         return True
   return ('a', 'b') if nested_method(a, b) else ('a', 'c') if nested_method(a, c) else ('b', 'c') if nested_method(b, c) else None

1 Comment

i mean is there a return return value or something like that, so that the nested function returns that the method needs to return the returned value
0

If I understand correctly you're looking for something like this:

def method(a, b, c):
    def nested_method(d, e):
        if d == e:
            return True

    for args in ((a, b), (b, c), (b, c)):
        if res := nested_method(*args) is not None: 
            return res # or return args

4 Comments

not exactly. in my code the nested_method is called much more often and returns an integer, so it would not work for me like that
He copy-pasted your code. It returns a boolean, not an integer. Maybe we're all trying to solve a problem different than the one you actually have.
@Ignatius Reilly the code written above is an example. it returns a boolean. but my actual code returns an integer
Basically I'm saying to make a list of the arguments you want to use, iterate over that list to call the nested method and for each iteration check if the nested method returned something other than None. If it does, then return that value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.