Why does python use modules, instead of just including the module functions in the main language. It would be very useful and pretty easy, especially for the main ones such as random, re, and os. If Python preaches simplicity and minimalist, why do you have to write in extra lines of code?
Why does python use modules, instead of just including the module functions in the
Share
1) Zen of Python #19: “Namespaces are one honking great idea — let’s do more of those!”
Named modules are good because they eliminate any chance of a collision between functions with the same name. If everything was a builtin, then
os.error()would collide withlogging.error()(and heaven forbid you try to define your own function callederror()!)Ditto the builtin
int()function and therandom.int()function. You would have to write the latter asrandom_int(), which is just as much typing as the module syntax. Why not make the namespaces explicit and use modules?This is the same reason the syntax
from os import *is frowned upon – it pollutes your namespace and introduces the chance for exciting name collision errors.2) Who decides what’s a builtin and what’s a module?
Most of the programs that you personally write involve
osandre. Personally every script I’ve written in the last three months has involvedsqlite3,csvandlogging. Should those be included as builtins for every program that any Python programmer ever writes?After a while your list of builtins gets to be bigger than Ben Hur.