Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a top-level statement in Python?

Tags:

python

In the Python Guide's chapter on project structure, the term "top-level statement" is brought up a few times. I'm not sure exactly what this refers to. My guess is it's any variable declarations that happen outside of any functions or class methods that fire as soon as a module is loaded. Is this correct? Does it also include a module's import statements?

like image 704
Steven Mercatante Avatar asked Aug 08 '13 23:08

Steven Mercatante


People also ask

What is the meaning of top-level statements in function?

Top-level statements enable you to avoid the extra ceremony required by placing your program's entry point in a static method in a class.

What are the types of statements in Python?

There are mainly four types of statements in Python, print statements, Assignment statements, Conditional statements, Looping statements. The print and assignment statements are commonly used.

What is __ name __ in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.

What is if name == Main in Python?

if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported.


2 Answers

It's not just variable declarations (and there aren't any variable declarations anyway). It's pretty much anything that starts at indentation level 0.

import sys         # top-level

3 + 4              # top-level

x = 0              # top-level

def f():           # top-level
    import os      # not top-level!
    return 3       # not top-level

if x:              # top-level
    print 3        # not top-level
else:
    print 4        # not top-level, but executes as part of an if statement
                   # that is top-level

class TopLevel(object): # top-level
    x = 3          # not top-level, but executes as part of the class statement
    def foo(self): # not top-level, but executes as part of the class statement
        print 5    # not top-level
like image 55
user2357112 supports Monica Avatar answered Oct 12 '22 23:10

user2357112 supports Monica


Here's the first mention of "top-level statement":

Once modu.py is found, the Python interpreter will execute the module in an isolated scope. Any top-level statement in modu.py will be executed, including other imports if any. Function and class definitions are stored in the module’s dictionary.

This makes it clear that what they really mean is "things that are interpreted at import time".

While it's not terribly helpful directly, the Python documentation itself also uses the phrase "top-level" (components, which then means "statements" in this context).

Note that this module:

"""a python module, spam.py"""

def spam():
    return "spam"

class Spam(object):
    pass

has two statements in it, the def and the class. These are both executed at import time. These definitions are compound statements (see def and class descriptions). If there are decorators attached to a top-level def, that adds even more top-level things to run. (See also user2357112's answer: running a class statement invokes more internal workings.)

Add an import sys at the top and you've added a third statement, which imports sys. However, if you add this:

def ham(eggs):
    import os
    return os.path.basename(eggs)

you have still only added one statement, the def ham, to the top-level stuff. It's when ham itself is executed (called) that the import os will be run.

like image 39
torek Avatar answered Oct 13 '22 00:10

torek