All Questions
Tagged with python coding-style
76 questions
310
votes
2
answers
539k
views
Python file naming convention?
I've seen this part of PEP-8 https://www.python.org/dev/peps/pep-0008/#package-and-module-names
I'm not clear on whether this refers to the file name of a module/class/package.
If I had one example ...
174
votes
23
answers
26k
views
Programming cleanly when writing scientific code
I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code.
My code is primarily "scripting" type stuff - things to test mathematical ...
135
votes
15
answers
34k
views
Is it always a best practice to write a function for anything that needs to repeat twice?
Myself, I can't wait to write a function when I need to do something more than twice. But when it comes to things that only appear twice, it's a bit more tricky.
For code that needs more than two ...
122
votes
2
answers
125k
views
What's wrong with relative imports in Python?
I recently upgraded versions of pylint, a popular Python style-checker.
It has gone ballistic throughout my code, pointing out places where I import modules in the same package, without specifying the ...
88
votes
6
answers
112k
views
Single quotes vs double quotes [closed]
I just started a job where I'm writing Python after coming from a Java background, and I'm noticing that other developers tend to quote strings using single quotes ('') instead of double quotes (""). ...
59
votes
6
answers
111k
views
Should I really use all uppercase for my constants?
I am a Python programmer primarily who uses pylint for linting source code. I am able to eliminate all of the warnings except one: Invalid name for a constant. Changing the name to all caps fixes it, ...
46
votes
8
answers
21k
views
How can I learn to effectively write Pythonic code?
Doing a google search for "pythonic" reveals a wide range of interpretations. The wikipedia page says:
A common neologism in the Python community is pythonic, which can have a wide range of ...
41
votes
6
answers
38k
views
Which style to use for unused return parameters in a Python function call
Is there any recommended/generally accepted coding style for handling situations where a function returns a tuple of values but only one of those values is used afterwards (note that this is mostly ...
36
votes
3
answers
39k
views
Is it ok to have multiple classes in the same file in Python?
In Java and PHP (although not strictly required), you are expected to write each class on its own file, with file's name is that of the class as a best practice.
But in Python, or at least in the ...
24
votes
8
answers
7k
views
Is using lambdas to express intent not pythonic?
PEP 8 states the following about using anonymous functions (lambdas)
Always use a def statement instead of an assignment statement that
binds a lambda expression directly to an identifier:
# Correct: ...
24
votes
6
answers
9k
views
Best practice for redundant conditions in if-elif-else statements
What is considered better practice?
Case 1:
if n == 0:
doThis()
elif n < 0:
doThat()
elif n > 0:
doSomethingElse()
Case 2:
if n == 0:
doThis()
elif n < 0:
doThat()
else:
...
17
votes
3
answers
14k
views
`var is None` vs `var == None`
Why do people prefer var is None over var == None when is can be used on few objects only?
17
votes
2
answers
3k
views
Creating nested functions for purely aesthetic reasons?
I've always wondered what other programmers think about the idea of creating pure aesthetic functions.
Say I have a function that processes a chunk of data: Function ProcessBigData. Say I need ...
15
votes
5
answers
19k
views
Is using nested function calls a bad thing?
In a recent homework assignment I ended up calling my functions in an ugly way uglyReceipt(cashParser(cashInput())) the program itself worked perfectly but I still felt like I was doing something ...
15
votes
3
answers
7k
views
Why is the _replace method of Python namedtuple classes protected?
In the section Method Names and Instance Variables, the Python Style Guide (aka PEP 0008) says, "Use one leading underscore only for non-public methods and instance variables." Why then, does the ...