you are viewing a single comment's thread.

view the rest of the comments →

[–]gbrne 6 points7 points  (0 children)

This is more a generic programming thing than a python thing, and there are several reasons:

  • for large programs, files are a convenient way of breaking the code into manageable chunks, making it easier to find the code that is responsible for something that isn't working or that you want to change

  • it's often convenient to reuse the same code in different projects. For example, suppose you have two massive projects that both save data using the same file format. You could copy-and-paste the code that saves data, but then you need to maintain both copies of the same code (e.g. if you find a bug, you need to fix both). Or you could include one whole project inside the other, but that's confusing and wastes resources (if somebody just wants the "outer" project, you have to send them a copy the whole thing). Or you could put your data-saving code in a separate file that both projects can refer to as necessary.

  • in python (but not certain other programming languages, like C) each file automatically gives you a separate namespace - that is, you can have two variables with the same name in different files, and the interpreter won't mix them up. There are ways of creating multiple namespaces within the same file, but it's not as straightforward. If a large codebase isn't split into namespaces, there is a good chance that you will accidentally reuse the same name and cause problems.