4

I'm sorry if this has already been answered. I didn't really know how to search for this particular question.

In C++ you can define a variable at the top to later define. For example:

int printOne();

int main()
{
     cout << printOne() << endl;
     return 0;
}

int printOne
{
     return 1;
}

I'm pretty new to Python though, so I was wondering if this was a possibility for Python as well.

Garfield Tong
  • 408
  • 4
  • 12
  • 1
    You don't need to predeclare functions in Python. The identifiers in the body of a function aren't resolved until the function is run. In C they are resolved when the function is defined, so you can't reference something that will be defined later. Not an issue in Python. – khelwood Feb 03 '17 at 15:15
  • Possible duplicate of [Declare function at end of file in Python](http://stackoverflow.com/questions/3754240/declare-function-at-end-of-file-in-python) – DrBwts Feb 03 '17 at 15:17

3 Answers3

4

you don't have to. Python evaluates everything at run-time:

def a():
    print(b())

def b():
    return 12

a()

so when a is called b is already defined.

note: that doesn't work because when a() is called b isn't defined yet:

def a():
    print(b())

a()

def b():
    return 12

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module1>", line 4, in <module>
  File "<module1>", line 2, in a
NameError: name 'b' is not defined
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Thanks so much. This helped. One last thing though, I don't call my definitions at the end of the file, as the way we're doing it, we open IDLE, import, reload, and then just type in the function we want to run. So instead of typing 'a()' at the bottom of the page, we just import it in IDLE, then type 'a()' Is it the same answer for this particular way of running functions too? – Garfield Tong Feb 03 '17 at 15:30
  • 1
    as long as it's imported (and if IDLE actually imports, not just displays the text), you can use the functions in the interpreter. Works with PyScripter (you have to _run_ the buffer to evaluate it, though, depends on the editor) – Jean-François Fabre Feb 03 '17 at 15:33
1

There generally isn't a need. The function only needs to be defined by the time you call the function. For example this would work just fine even though the definition of printOne was after main.

def main():
    print(printOne())

def printOne():
    return 1

main()
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

I found this useful when I want to define all my parameters at the top, but their computation depends on not yet declared code:

# Parameter section ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
param_a = 2
# Like to set param_b here for everyone to see, but it depends on big complex functions:
param_b = lambda: compute_param(param_a)
param_c = 'compute_param(param_a)'

# Function section ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def fn(b = param_b, c = param_c):
    ''' Function using the global parameters defined at the top 
    '''
    if callable(b): 
        b = b()
    c = eval(str(c))
    print(b + c)

def compute_param(x):
    return 3*x

# MAin code section ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if __name__ == '__main__':
    fn() #default params
    fn(10,10)