Vakataka

Install

Vakataka, a tap away.

Add it to your home screen for a quicker way back to your next taka.

iPhone or iPad
Open in Safari, tap Share, then Add to Home Screen. Choose Open as Web App if shown, then Add.
Android
Open your browser menu and choose Install app or Add to Home screen.
Computer
Use the install icon in the address bar or your browser’s app menu. In Safari on Mac, choose File → Add to Dock.

If your browser has no install option, Vakataka still works here.

Why importing a Python file can run code

An import can execute statements at the module’s top level.

A Python module is a file that can provide definitions for another program. When it is first imported in an ordinary interpreter session, Python executes its top-level statements. Defining a function makes that function available; it does not by itself run the function’s body.

Top-level demonstration code is different. A print statement or file operation there can run during import, surprising code that only wanted a function. Keeping definitions separate from demonstration or startup actions makes modules easier to reuse and reason about.

A small example

A file defines a calculation function and then prints a sample result at top level. Importing the file can produce that sample output.

Keep in mind

Python normally caches imported modules; do not assume every repeated import reruns the file.

Source & attribution

Introduction to Python Programming — Top-level code

OpenStax contributors · OpenStax, Rice University

CC BY 4.0. Rewritten as a self-contained explanation; the example and reflection are Vakataka additions. Written and edited with AI assistance. No source images reproduced.

Source edition: 2024-07-30T19:17:56Z

Select the link to copy it.

Email · WhatsApp

A note to yourself

Create an account or sign in to keep private notes and saved takas across devices.