Need Assistance?

support@tutorialshub.in

Functions and Modules

Lambda and higher-order functions

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Modules give code a namespace

A module is a Python file containing definitions that other code can import. Modules prevent large applications from becoming one enormous file and provide a natural place to group related functions, classes and constants.

Import explicitly

Prefer clear imports that show where a name comes from. Avoid wildcard imports because they make dependencies difficult to see and can overwrite names unexpectedly.

Keep import-time behavior safe

Code at module level can run when the module is imported. Reusable modules should therefore avoid surprising side effects such as starting servers, deleting files or performing expensive operations simply because another module imports them.

Practice: create a utility module with reusable calculation functions and a separate script that imports and uses them.

ADVERTISEMENT