-1

For example:

def foo():
    def bar():
        return

    # some code
    return

def foo2():
    # call bar() here?

Is it possible to put bar() inside foo() and call it in foo2()?

khelwood
  • 55,782
  • 14
  • 81
  • 108
THN
  • 3,351
  • 3
  • 26
  • 40
  • 4
    No, that's not possible. What are you actually trying to do? – Tomalak May 30 '20 at 06:30
  • @Tomalak I want to expose some of the logic of `foo()` to other methods without refactoring out its code. – THN May 30 '20 at 06:31
  • no it is not possible – eli May 30 '20 at 06:31
  • If `foo` returns `bar`, then it could be passed to `foo2` and called there. If `foo` has enough context used by `bar` to warrant this kind of thing, I think you should consider putting `foo` and `bar` in a class and sharing state (or code) that way. – cco May 30 '20 at 06:33
  • 2
    In theory, you could dig into `foo.__code__.co_consts`, but in practice - don't. – bereal May 30 '20 at 06:36
  • *"expose some of the logic without refactoring"* - Refactor your code is all I can say. – Tomalak May 30 '20 at 06:37
  • 4
    "I want to expose some of the logic of foo() to other methods without refactoring out its code." And why don't you want to refactor out its code? That's... essentially what refactoring is *for*. – Karl Knechtel May 30 '20 at 06:39
  • @jonrsharpe This is the first time I ask this, not sure what you are talking about. – THN May 30 '20 at 06:42
  • @KarlKnechtel I'm just curious about it. But to give a reason, maybe it's for the same benefit of defining a method inside a method, to make it self contained and not fragmented(?) – THN May 30 '20 at 06:45
  • @jonrsharpe I see what you mean, it seems search failed me. The fact that I couldn't find the duplicate suggests that other future searchers may benefit from my question and the discussion here. – THN May 30 '20 at 06:54
  • Given that I got a bunch of hits from just sticking the title into Google, I don't think it suggests that. – jonrsharpe May 30 '20 at 06:56
  • @jonrsharpe Most of my search hits are about trivial cases of calling method in class. Another reason may be because I searched method instead of function. So given a specific context and a considerable time, search can fail. Thanks for your efforts to link duplicates, it will help future searchers to jump to those duplicates. – THN May 30 '20 at 07:07

1 Answers1

4

Somewhat, you can do that - by declaring bar global:

def foo():
   global bar
   def bar():
      print("In bar")

But you cannot call bar unless you first call foo, because that's the function that defines bar:

bar()
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#NameError: name 'bar' is not defined    
foo()
bar()
#In bar

Overall, this is a very bad idea.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 2
    I agree it's a very bad idea. Looks like refactoring out the code is the way to go. – THN May 30 '20 at 06:40
  • 1
    Not to mention what happens to any closure variables... – Karl Knechtel May 30 '20 at 06:53
  • 1
    For futurers: closure variables are variables that `bar()` has access through `foo()`'s scope. This is a technique in functional programming to generate different functions (i.e., different `bar()`) with different "states", similarly to private variables in OOP. – THN May 30 '20 at 07:45