0

I've been struggling with Python in VS Code because my functions weren't being recognized.

I tried following a bunch of stuff in this Q&A: No definition found for function - VSCode Python. I also tried messing with the interpreter, the python settings, updating to Python 3.11, different environments and nothing worked.

Then I discovered when running this, it works totally fine.

def hello():
    print("Hello World!")

hello()

But when running this, it gives an error that hello() isn't defined

hello()

def hello():
    print("Hello World!")

It's been a few years since I've done any coding with Python but this feels weird. I'm used to this logic in Powershell, but everywhere else I'm used to it not caring where it's defined as long as it is defined properly.

Unless this is something related to it being a basic script file that doesn't have a main() function. It's a script that I only plan to run from VS Code, so if I have to define my functions before invoking them I can do it that way. I just prefer to put my functions at the bottom because I'm usually writing them as I'm figuring things out and breaking things into smaller parts.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
dbarnes
  • 439
  • 1
  • 3
  • 11
  • 2
    Python is executed top to bottom, and function definitions are just variable assignments – Brian61354270 Aug 19 '23 at 02:48
  • 1
    This is not specific to VS Code. This is just plain Python behavior. You can replicate that same "*NameError: name 'hello' is not defined*" error by running your 2nd Python script (with `hello()` called before its definition) from a terminal _outside of VS Code_ / without using VS Code at all. – Gino Mempin Aug 19 '23 at 02:55
  • 2
    Does this answer your question? [Is it possible to call a function before defining it in python?](https://stackoverflow.com/questions/58306444/is-it-possible-to-call-a-function-before-defining-it-in-python) – Gino Mempin Aug 19 '23 at 02:58
  • either I never got deep enough into Python when last I was using it, or I was using a main() function call – dbarnes Aug 19 '23 at 22:26

2 Answers2

1

Python is interpreted from the top to the bottom, meaning that functions must be defined before they are called. This is true for all python scripts, not just ones run on VS Code.

Python Nerd
  • 171
  • 1
  • 9
  • Emphasis on **_not just ones run on VS Code_**. There seems to be a common misunderstanding recently on what VS Code does to run Python code, when it basically just follows the same rules as doing `/path/to/python file.py`. People should first understand how to run Python codes _outside of VS Code_ before actually using VS Code. – Gino Mempin Aug 20 '23 at 04:27
0

Python compiles the module and then executes it starting at the top. Looking at the disassembly with line numbers on the left

  1           0 LOAD_NAME                0 (hello)
              2 CALL_FUNCTION            0
              4 POP_TOP

  3           6 LOAD_CONST               0 (<code object hello at 0x7f3ee7adeef0, file "test.py", line 3>)
              8 LOAD_CONST               1 ('hello')
             10 MAKE_FUNCTION            0
             12 STORE_NAME               0 (hello)
             14 LOAD_CONST               2 (None)
             16 RETURN_VALUE

the already-compiled function <code object hello> isn't stored to the global variable hello until line 3 (STORE_NAME 0 (hello)). That is after the LOAD_NAME on line 1 that causes the fail.

The only rule is that the name "hello" must be resolved to a function object at the time of the call.

tdelaney
  • 73,364
  • 6
  • 83
  • 116