Leonard
Leonard

Reputation: 3208

Python: What does parenthesis () after a variable name mean?

Towards the bottom at the main loop, I'm seeing this line

result = result()

But I have no idea what it does and I can't even Google it. What is this?

The code below imports from task.py and project.py. But both files do not have anything related to result() hence I'm not including them here.

#!/usr/bin/env python3

from task import Task
from project import Project

main_menu = {
    'title': 'MAIN MENU',
    'items': ['1. Create a project', '2. Load a project', '3. Quit'],
    'actions': {
        '3': exit,
    }
}

project_menu = {
    'title': 'PROJECT MENU',
    'items': ['1. Add a task', '2. Add task dependency', '3. Set task progress',
            '4. Show project', '5. Back to Main Menu'],
    'actions': {
        '5': main_menu,
    }
}

def select_menu(menu):
    while True:
        print()
        print(menu['title']) #MAIN MENU
        print('\n'.join(menu['items'])) #1. create project, 2. load project ..
        selection = input('Select > ')
        next_action = menu['actions'].get(selection)
        #print(selection, menu['actions'])
        if next_action:
            return next_action
        else:
            print('\nPlease select from the menu')

def create_project():
    global cur_project
    global project_menu

    project_name = input('Enter the project name: ')
    cur_project = Project(project_name)

    return project_menu

main_menu['actions']['1'] = create_project

cur_menu = main_menu
cur_project = None

while True:
    result = select_menu(cur_menu)

    while callable(result):
        result = result()

    cur_menu = result

Upvotes: 1

Views: 2139

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

You have to see the global loop:

while callable(result):
    result = result()

it just calls result function until it returns a non-function (probably a result), reassigning back the result name. result is just a name, it can reference anything including a function.

you can see that like traversing a tree node by node until you reach a leaf.

Upvotes: 1

Amadan
Amadan

Reputation: 198324

select_menu returns an element of actions, which are all functions (main_menu, exit, create_project....). Thus, result is a function. result = result() will execute that function and replace the value in result with the return value of that function.

Upvotes: 2

Related Questions