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 2 years ago.
Improve this question
I'm new to python and I've been wondering if it is bad practice to call main from another function to essentially re-run the program if certain conditions do not apply.
Here's a code example.
def main():
userInput = input("Please input a word(\"terminate\" exits): ")
if(len(userInput)>=5):
wordLength(userInput)
else:
wordLength()
def wordLength(word = "Default print"):
if (word != "Terminate"):
print(word)
main()
if __name__ == "__main__":
main()
Is this bad practice and should I do it in a different way than this? Perhaps with a while loop?
You should use a while loop here. It is not "bad" to call main from another function, but this pattern is not widely used and would be difficult for future maintainers (even hypothetical ones) to read and understand.
It is also generally not recommended to have this type of cyclical recursive function call if at all avoidable for the same reason as well as adding even more memory to the stack.
Keep in mind that there is an edge case and exception to almost all established design rules and patterns (those listed here included), so it is a good idea to ask for second opinions when you think you might to "reinvent the wheel."
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 7 months ago.
Improve this question
What are some acceptable use cases for using while true. I’ve heard that it’s not good practice to use while true and break. For simple use cases like let’s say a rps game, would it be worth using. If not, I’m considering to use boiler plate code.
You can use while(True) in such conditions when you need to make the code running until interrupted by the person running the code.
A use case can be fee submitting system. The program needs to be running for the entire day. There are a lot of other use cases too.
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 1 year ago.
Improve this question
I'm sorry if I'm not asking this correctly. It may be due to a lack of understanding.
If I was explaining to someone how to understand of a piece of code that's been written by someone else..I would say something like:
"Ok so here we have this method getting called and it's taking one argument. Let's find the method declaration itself and see what it's doing and what it's doing to our argument."
Is there a concise way of describing looking at code backwards in a way? Following the sequence of events? Flow of execution? Flow/order of operations?
Thank you.
My professors refer to it as tracing a function. It might be linked to localisation (I'm in France and translating their word into English) or to a specific language, but that's what I got and it sounds right.
I think this bit of Python doc supports my proposed answer.
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 2 years ago.
Improve this question
I made a package that I've refactored following Uncle Bob's clean code book.
The result: I have a ton of smaller functions from the giant functions I had before (as I'm still learning Python & best practices).
My question is this:
To keep things organized...
On functions you won't use other places...
is it better to use nested functions
def public_func():
def private_func():
# do abc
private_func()
# do xyz
or have a lot of smaller functions with the private function syntax?
def _private_func():
# do abc
def public_func():
_private_func()
# do xyz
As the Zen of Python says:
Flat is better than nested.
But also:
Simple is better than complex.
Complex is better than complicated.
Therefore, try to not use nested, but if it makes it more complicated, keep it simple.
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 3 years ago.
Improve this question
I'm writing a module which only contains functions. Is it good practice to put these inside a class, even if there are no class arguments and the __init__ function is pointless? And if so how should I write it?
There's no particular reason to force functions to go inside a class if there's no reason to make a class. A python sourcefile already provide a perfectly reasonable namespace for storing similar, related functions. The pypy implementation of the python programming language, for example, has many modules that don't have classes in them (for example, time).
You would want a certain data structure as a class, of course, but there are certain behaviors that are entirely independent of data structures, and putting them in a class would just add needless complexity.
It is good to build modules that contain a class for better organization and manipulation depending on how big the code is and how it will be used, but yes it is good to get use to building classes with methods in them. Can you post 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 4 years ago.
Improve this question
is it a good practice that i usually (tend to) do in python?
making shortcut of functions for example:
p = print
and the i use:
p('hello world')
or
p(2**5)
does it affect on performance? What is your suggestion, is it OK?
A good practice in Python is: easy is better. You do not give much clarity in your code by doing that (and depending on the IDE-highlighting strategy, make it even worse for others). Furthermore, the choice of your shortcut, namely p, is really a bad idea. How about people writing code with prime numbers (and P is not a good idea too).
For performances, there is no gain I think. Since print is a built-in function, you actually add a variable inside globals() or locals(). In my opinion, this is 1) not a good practice to use shortcuts like that 2) not an improvement in performance.
I have no idea if it is a good practice in python, but in general I would say no.
It will take away readability for other readers of your code. If the codebase grows large and you replace a lot of functions with very short names you make it difficult to read in general.
Coming from many years of code maintenance if I was called out in the middle of the night to fix a code problem in production and found code like that I am pretty sure I would hunt you down in the morning and ask why you would ever do such a thing.
If you are lazy and doesn't want to write so much - get your self an editor that can complete code for you instead.