0

Question is simple. I just want to call function2 in this code. How can ı do? so I want to print text in this code.

def func1():
    def func2():
        print("test")
khelwood
  • 55,782
  • 14
  • 81
  • 108
enes durmuş
  • 47
  • 2
  • 7
  • 4
    You can only call `function2` when inside of `function1` (unless you explicitly make it available outside), and in that case, it's like any other function call. Please give more detail about how you expect it to be used. – Carcigenicate Dec 25 '20 at 14:36
  • 1
    You want to call `func2` from inside `func1` or from outside? – khelwood Dec 25 '20 at 14:36
  • 1
    Does this answer your question? [Call Nested Function in Python](https://stackoverflow.com/questions/11154634/call-nested-function-in-python) – IoaTzimas Dec 25 '20 at 14:37
  • That is not [easily] possible, or a good idea. Because `func2` is no longer required only locally, you should encapsulate `func1` and `func2` inside a either class or module so that they exist within an appropriate scope. – Ninjakannon Dec 25 '20 at 16:04
  • This should be easily possible via a number of ways (making it a "static" function of the function, returning the function, assigning it to a global, making the function a custom callable class...). Which way is appropriate for them depends on their use case though. – Carcigenicate Dec 25 '20 at 16:53

2 Answers2

3

This is what you need:

def func1():
    def func2():
        print("test")
    func2()

And then call func1:

>>> func1()
test
Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
0

If you need exactly you wrote:

def f1():
    def f2():
        print("test")
    f2()
f1()

But maybe you meant a class:

class c:
    def f():
        print("f()")
Jenia
  • 374
  • 1
  • 4
  • 15