Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are one-line 'if'/'for'-statements good Python style?

Every so often on here I see someone's code and what looks to be a 'one-liner', that being a one line statement that performs in the standard way a traditional 'if' statement or 'for' loop works.

I've googled around and can't really find what kind of ones you can perform? Can anyone advise and preferably give some examples?

For example, could I do this in one line:

example = "example" if "exam" in example:     print "yes!" 

Or:

for a in someList:     list.append(splitColon.split(a)) 
like image 683
Federer Avatar asked Nov 13 '09 12:11

Federer


People also ask

Are one line if statements OK?

Only use single-line if statements on a single line While the compiler sees this as one statement guarded by a single condition, humans often accidentally read this is an if block, whether there are curly braces or not, thanks to the indentation. Humans notice the indentation, the compiler does not.

Can you do a one line if statement in Python?

Python If Statement In One LineIn Python, we can write “if” statements, “if-else” statements and “elif” statements in one line without worrying about the indentation. In Python, it is permissible to write the above block in one line, which is similar to the above block.

How do you break a long if statement in Python?

The recommended style for multiline if statements in Python is to use parenthesis to break up the if statement. The PEP8 style guide recommends the use of parenthesis over backslashes and putting line breaks after the boolean and and or operators. Copied!

Can you have two for statements in one line in Python?

These statements can very well be written in one line by putting semicolon in between. However, this practice is not allowed if there is a nested block of statements.


2 Answers

Well,

if "exam" in "example": print "yes!" 

Is this an improvement? No. You could even add more statements to the body of the if-clause by separating them with a semicolon. I recommend against that though.

like image 97
Stephan202 Avatar answered Oct 12 '22 23:10

Stephan202


I've found that in the majority of cases doing block clauses on one line is a bad idea.

It will, again as a generality, reduce the quality of the form of the code. High quality code form is a key language feature for python.

In some cases python will offer ways todo things on one line that are definitely more pythonic. Things such as what Nick D mentioned with the list comprehension:

newlist = [splitColon.split(a) for a in someList] 

although unless you need a reusable list specifically you may want to consider using a generator instead

listgen = (splitColon.split(a) for a in someList) 

note the biggest difference between the two is that you can't reiterate over a generator, but it is more efficient to use.

There is also a built in ternary operator in modern versions of python that allow you to do things like

string_to_print = "yes!" if "exam" in "example" else "" print string_to_print 

or

iterator = max_value if iterator > max_value else iterator 

Some people may find these more readable and usable than the similar if (condition): block.

When it comes down to it, it's about code style and what's the standard with the team you're working on. That's the most important, but in general, i'd advise against one line blocks as the form of the code in python is so very important.

like image 29
Bryan McLemore Avatar answered Oct 12 '22 23:10

Bryan McLemore