Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a python source file with a few classes in it. These classes require roughly the same imports, and rely on each other: each references some of the others in its methods.
Semantically speaking, these classes form a single module. They belong in the same file.
The problem is that they have grown. Each class now spans well over a hundred lines, and that's after refactoring to keep their methods simple. The file is slow to navigate, often modified at different points, and hard to keep track of mentally.
If I break all this into smaller files, I'll find that each file has the same imports, and that they must import each other circularly.
How can I modularize this code?
Note: the fact that these classes are large is not outright bad design. They are message dispatchers that have one method for each kind of message they can handle. There is a hierarchy in place that allows for code reuse via inheritance wherever possible.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 months ago.
Improve this question
If we talk about resources, memory, legibility what would be better between:
Create all the instances at the beginning of a method? (left side of the screenshot)
Create the instances as we need to use them? (right side of the screenshot)
Do you have any documentation that says which is better? I searched in the Python documentation but found nothing.
Well in python creating instances is creating instances, regardless of where you do such, the same amount of instances will be created taking the same amount of storage space.
If you're looking into memory conservation I'd recommend using the del operator when instances are no longer needed.
Other than that this is totally just up to personal preference, and how you'd like to format your code.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
TL,DR: Is it a sacrilege to number your code source files in their executing orders ?
I'm developing a very small program in Python, which execution is very sequential.
Kinda looks like a script.
I thought about numbered the source files and librairies by their order of execution, when possible. To me it brings the same clarity in the folder organization, exactly as you would number the chapters of a book...
For transversal functions that can be called at any step of the program they are in files named by their functionalities.
Is it an absolute don't do? Why ?
here is an example of how my project folder looks:
Project_0_main.py
Project_1_step_yyy.py
Project_2_step_xxxx.py
Project_3_step_zzzzz.py
Project_math_functions.py
Project_do_that_function.py
Thanks
The only question that matters is about maintainance. If you are sure that you could possibly adapt steps but never add a new step between 2 other ones, then numbering is fine, even if is is not the most common use(*). It you cannot be sure of that, then you have just find the don't and the reason why.
It is not a absolute don't, but naming scripts with sensible names related to the real actions is a form of minimal, implicit and immediate documentation. Using numbers will lose that, and would require an annex file to document what is done in each numbered step.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry if the title is confusing.
What I am trying to say is this:
I have worked with Python before, but I'm by no means an expert. So far everything I have done has just been 'somefile.py' with lots of methods and code in it, but it doesn't really have any organizational structure. In Java (which I am more familiar with than Python), there are usually different classes that each have methods and are called from each other. How do you make a file full of code organized and structured when working on a large project? Break them up into files by class?
Hopefully this is clearer. Let me know if this needs clarification.
In Python, the file unit is called module. Modules are organized in packages.
You usually put your classes each in a module and also use modules to group related code that doesn't belong to any class. Related modules are grouped in packages (physically represented by directories) which effectively create namespaces.
Then you use the import command to import the desired pieces of the code into other modules.
You can read about modules, packages and import in the Python documentation here.
Logically, it isn't much different than Java or other languages.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
New programmer here!
I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.
I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?
I'm using Python 2.something if that makes a difference.
A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.
That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.
If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.
However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Im not positive on the terminology here, so that may explain why searching on my own yielded no results.
I was just curious if there is a widely accepted, general method to writing a module in python. Obviosuly people prefer splitting things into segmented .py scripts, importing when needed, and packing it all into a folder.
What I want to know: Is there a general method to how/when/why we stop writing things together in one .py and begin a new one (And i mean other than obvious things like... one script .py for the main job, and then a preferences.py to handle reading/writing prefs)
You should split your code into multiple modules when it begins to be unwieldy to keep it all in one module. This is to some extent a matter of taste. Note that it may unwieldy for the code author (i.e., file is too big to navigate easily) or for the user of the library (e.g., too many unrelated functions/classes jammed together in the same namespace, hard to keep track of them).