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.
Related
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 months ago.
Improve this question
How to make a discord bot so when I input the following it calculates it:
INPUT:
2.2x100 2.8x100-10%
OUTPUT:
output should be the result of the following:
2.8100 - 2.2100 and then minus 10% of that,
(that equation is subject to change)
How would I do this?
This is not an easy task (if you don't want to be unsafe and eval() code).
You need an infix parser that takes into account order of operations (priorities). If there are parenthesis in your operation, it will be even more complex.
You will then need to evaluate the result with a function that executes an abstract syntax tree generated by the parser (AST).
There is probably libraries that make this easier without being unsafe, but make a google search about all the terms I talked about so you can learn more.
EDIT: also take a look at this link. Not beginner stuff.
Infix Calculator Expression Parser
EDIT 2: Just in case you want the easiest solution with eval(), be careful. A skilled attacker can destroy your computer with malicious code. If you just eval() whatever is sent to the bot, they could really destroy / take control of your computer. Be careful.
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 5 years ago.
Improve this question
What are the official guidelines for the location of the main method in a Python program?
Should it be written first (at the top of the program), last (at the bottom), or is it just preferential? I read through PEP8 and couldn't find anything.
There are no official guidelines referring to this.
Whatever is most useful for someone to read first should go first.
Often, this is going to be some kind of "main" method. It describes the flow of the program. It will give developers a quick way of figuring out what is happening and where to look for things they want.
But I can envisage cases where it's not explicitly the main() method. Maybe the main() method just spins off some threads and it's what's inside it that counts. Maybe main() just does a bunch of argument parsing, and then calls another function. Maybe main() is just boilerplate.
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
So I've been thinking to make a programming language written in python but I have no idea where to start, and I really don't know the steps creating it.
You would probably start by first planning out your language. That should take a lot of time. Then in Python... you would write a parser that can understand the syntax of your language, and an interpreter that can take the results of the parser and perform the actions of the program.
Your language that is written in Python with Python in-turn being written in C is practically guaranteed to be very slow and will not succeed, but this could be a really fun thing to do as a learning or education experience.
You will likely want to look at Abstract Syntax Trees. This is the underlying structure that python is built on. Take a look here at the documentation: https://docs.python.org/2/library/ast.html
Using ASTs you can at least define the syntax of your language. You will still need to solve the problem of how to interpret it on a platform to get your code to execute.
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).
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.