Questions tagged [zen-of-python]

Related to python best practices. A set of guiding principles for Python's code design comprising 20 aphorisms, only 19 of which have been written down; Created by Tim Peters.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

easter egg: >>>import this

https://www.python.org/dev/peps/pep-0020/

13 questions
23
votes
8 answers

Why does Python treat tuples, lists, sets and dictionaries as fundamentally different things?

One of the reasons I love Python is the expressive power / reduced programming effort provided by tuples, lists, sets and dictionaries. Once you understand list comprehensions and a few of the basic patterns using in and for, life gets so much…
Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
11
votes
1 answer

Python Zen - (only) one way to do it

This question might be sound subjective, but as "the Zen" says, there is (nearly always) one way to preferred, it shouldn't be subjective at the end. What way is the better one? [i.something() for i in l] map(operator.methodcaller('something'),…
glglgl
  • 89,107
  • 13
  • 149
  • 217
10
votes
5 answers

zen of Python vs with statement - philosophical pondering

I don't intend to simply waste your time, but: has it occurred to you too, while using Python's with statement that it really is contrary to the 5th line of "The Zen of Python" that goes "Flat is better than nested"? Can any enlightened Python guru…
NeuronQ
  • 7,527
  • 9
  • 42
  • 60
8
votes
3 answers

Zen of Python 'Explicit is better than implicit'

I'm trying to understand what 'implicit' and 'explicit' really means in the context of Python. a = [] # my understanding is that this is implicit if not a: print("list is empty") # my understanding is that this is explicit if len(a) == 0: …
4
votes
3 answers

What is preferable way to handle exceptions?

If I'm not sure at something while writing some code I'm trying to read The Zen of Python once more. For this time those lines hesitate me. Errors should never pass silently. Unless explicitly silenced. At current code I have some functions which…
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
2
votes
3 answers

Most elegant way to store (persist) the value of a single (numeric) variable

I need to store the value of a counter between executions of a script, so I can trigger a specific subroutine every hundred counts. I know I can write my integer into a text file and re-read it, and I know I can pickle my variable to much the same…
1
vote
1 answer

What is the PEP8 style recommendation for importing local functions?

PEP8 dictates that you put your imports at the top of your code, which allows the reader to see what you are importing in one space. However if you have a local repo for functions in order to import them you must first change your current…
Violatic
  • 374
  • 2
  • 18
1
vote
3 answers

Should I use builtin exception or define my own?

I've method or function like: findSomething(v) Is appropriate to raise KeyError in the case I don't find anything or it's better to define my own exception? What do you think? I know, it isn't strictly technical question, but they said:…
Maciej Wawrzyńczuk
  • 860
  • 1
  • 8
  • 19
0
votes
1 answer

Is there a special philosophy behind the zen of python being a cipher?

Before checking this.py I was expecting the Zen of python guidelines and a print statement. I found the following instead which consists of a cipher that is translated at runtime when you run import this. Is there a reason behind the string being a…
watch-this
  • 1
  • 4
  • 20
0
votes
1 answer

Refactoring a python project that has a lot libraries that have "from smthng import *"

I start a python project that continues developing. It has 10 local libraries. Each of them call each other "from name import *". For a example from main: from name3 import * from name2 import * from general_functions import * from messages import…
0
votes
1 answer

Most pythonic way to refactor multiple equality checks

Given an if statement like: if response.status == SUCCESS or \ response.status == FAILURE or \ response.status == CLEAR or \ response.status == READY: Is it better to refactor like (1): if any(response.status == status for status in…
0
votes
1 answer

With-statement with several variables: is there a more readable way to write this?

For example, I need to open two files using one with statement. And there's also a condition for each of them: to actually open or to use something instead. In my case, it's whether to open a file specified by name or use stdin/stdout if name is…
0
votes
1 answer

Pythonicly eliminating duplicates from list of hashable elements

I just thought of doing mylist = list(set(mylist)) to remove all duplicate entries from mylist. However, chaining built-ins always feels a little hacky. I am wondering, what is the (most) pythonic/zen way of eliminating duplicates from a…
user1129682
  • 989
  • 8
  • 27