How to Creating text File using Python?

When designing a python application, when should I create another class vs creating another .py file?

  • For example, I get the semantics of when I should create a separate class, but as far as I can tell I'm best off by just creating one large .py file. Assume the project I'm working on is being built from scratch (ie not using a framework that already has multiple .py files)

  • Answer:

    Separation at a file rather than class level is just another level of abstraction to use at the developer's discretion. Organize the package depending on what makes sense. Take a look at https://github.com/mitsuhiko/flask/tree/master/flask or https://github.com/facebook/tornado/tree/master/tornado as an example. Could you tell me what is in http://config.py, , http://logging.py? Probably. On the other hand, some people prefer keeping everything in one file. For example: https://github.com/defnull/bottle/blob/master/bottle.py In , how do you tell where the settings are stored? Logging? Routing? Bottle still groups related classes or functions together, but uses a large comment header instead of file. Is it any better?  I'd argue that the single file format hurts discoverability. To quote this Stack Overflow answer: http://stackoverflow.com/questions/106896/how-many-python-classes-should-i-put-in-one-file A module is a distinct thing that may have one or two dozen closely-related classes. The trick is that a module is something you'll import, and you need that import to be perfectly sensible to people who will read, maintain and extend your software. The rule is this: a module is the unit of reuse. You can't easily reuse a single class. You should be able to reuse a module without any difficulties. Everything in your library (and everything you download and add) is either a module or a package of modules.

William Ting at Quora Visit the source

Was this solution helpful to you?

Other answers

Creation of classes, and any other structures, should be driven primarily by efficiency. During refactoring you can establish methods to hide proprietary elements. Efficiencies can take different forms depending on your situation. One is processing efficiencies to make your application operate more quickly. Reuse of code is another. If you're in a large group or complex environment, creation of structures that establish group standards can have very significant advantages. With established standards, that have already been cleanly coded, other developers in the group can focus on unique aspects of new applications. I would advise the latter approach, even if only for personal use, assuming no departmental standards exist. If you've already coded and debugged a general structure for some function, you can reapply it to new situations. At some point you could well create a patentable behavior.

Jacque Swartz

The issue is always about modularity and levels of abstraction.  When you can group classes into a distinct, named grouping, put it in a separate file.  When you cannot, wait until you can.

Mark Janssen

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.