Lesson 15 of 21 · 50 min · UG / professional
The program door, hiding names, import as
Importing fees.py must not print a receipt. if __name__ == "__main__" is the door. _name and __all__ tell from * what to copy. import pandas as pd is a nickname, not a second library.
Application blueprint
A program is folders, config and a main door.
You will be able to
- ✓ Put launch prints behind if __name__ == "__main__" so import stays silent
- ✓ Know _name and __all__ as hints for from *
- ✓ Treat import pandas as pd as a nickname, not a second library
Importing fees.py must not print a receipt
When Python loads a file, it sets __name__ to "__main__" if a human (or a scheduler) launched that file, and to the module name if someone imported it. if __name__ == "__main__": is the door: the only block that prints a hostel receipt or talks to a CSV path. Helpers above the door stay importable. Tests import late_fee and never hit the door.
_late_table is a convention: please do not from fees import * that name. __all__ = ["late_fee"] tells from * what to copy on a laptop. You still should not use star imports in app code. The underscore is a hint to humans and to star; it is not a lock.
Laptop: door, __all__, import as
__all__ = ["late_fee"]
def late_fee(days):
if days <= 0:
return 0
return 25
def _debug_table():
return [0, 25, 50]
if __name__ == "__main__":
print(late_fee(4))
import as is a nickname
import pandas as pd does not install a second pandas. It binds the same module to a shorter name. import rsil_fees.late as late is the same idea for your package. Nicknames belong in one style: pd, np, plt — or the full name. Mixing both in one file is how a reviewer loses the thread.
This sandbox cannot set __name__. You practise the shape: functions first, then a print block you would indent under the door.
Words that matter
- Program door
- if __name__ == "__main__" — runs only when this file was launched.
- __all__
- Laptop list of names from * should copy.
- import as
- A nickname for the same module — not a second library.
Common mistakes
Avoid: print at import time so “you can see it working”.
Do this: Helpers return; the door prints.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — helpers stay quiet
Defining is not printing. loaded is the only line — like a successful import.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — the door is the only launch print
This block is what you would put under if __name__ == "__main__".
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — __main__ and aliases
The door keeps helpers importable. import fees as f is a nickname for the same file.
stdlib — no pip
Quiet import, loud door, one alias.
Real library code (not run in this browser sandbox)
# app.py
import fees as hostel
if __name__ == "__main__":
print(hostel.late_fee(4))
Example program — Nickname as data, same function
pd-style alias is a name you choose. The function is unchanged.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Door prints the fee
Define late_fee as 25 for 4 days. Print Launch and then the fee so 25 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.