Search code examples
pythoncurses

Why does function run without paramater?


I'm currently learning curses in python, and I found this piece of code online that is confusing me.

import curses

def draw_menu(stdscr):
    # do stuff
    # if you want more code just let me know


def main():
    curses.wrapper(draw_menu)


if __name__ == "__main__":
    main()

When I run this I don't get the expected missing 1 required positional argument error, since there is no parameter being passed in the curses.wrapper(draw_menu) line. Is this a curses thing? Any help is greatly appreciated.


Solution

  • A function is a datatype, just as much as strings, integers, and so on.

    def my_function(txt):
      print(txt)
    

    here type(my_function) # => <class 'function'>

    You invoke the code inside the function when you call it with parenthesis : my_function('hello') # => prints hello

    Until then you can perfectly pass a function as an argument to another function. And that last one can call the one you passed giving it some parameters.

    Like in your case, I'd guess that curses.wrapper() creates a screen interface that it passes as argument your draw_menu() function. And you can probably use that screen object to build your curse app.

    See this : Python function as a function argument?