Skip to content

Commit 9553d28

Browse files
committed
fixed example order
1 parent 1f71db9 commit 9553d28

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

README.md

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -188,44 +188,69 @@ end
188188
End each file with a newline. Don't include multiple newlines at the end of a file.
189189

190190
## Conditional Expressions
191+
191192
### Checking Truthy/Falsy
193+
192194
Prefer truthy/falsy checks vs comparing actual values.
193195

196+
"truthy" values are all objects except
197+
198+
1. `False`
199+
1. `None`
200+
1. `0`
201+
1. `[]` empty lists
202+
1. `()` empty tuples
203+
1. `{}` empty dictionaries
204+
1. an object whose magic method `__bool__` returns falsey(in python 3)
205+
1. an object whose magic method `__nonzero__` returns falsey(in python 2)
206+
207+
194208
```python
195209
# bad
196210
if value > 0:
197211
...
198212

213+
# good
214+
if value:
215+
...
216+
199217
# bad
200218
if value is 0:
201219
...
202220

203-
# bad
204-
if len(a_list) > 0:
221+
# good
222+
if not value:
205223
...
206224

207-
# bad
208-
val = None
209-
if val is None:
225+
# bad
226+
if len(a_list) > 0:
210227
...
211228

212229
# good
213-
if value:
214-
...
215-
216-
# good
217-
if not value:
230+
if a_list:
218231
...
219232

220-
# good
221-
if a_list:
233+
# bad sometimes
234+
val = None
235+
if val is None:
222236
...
223237

224238
# good
225239
val = None
226240
if not val:
241+
...
242+
```
243+
244+
If you need to check an object if it is `None` and not falsey use the `is` operator
245+
246+
```python
247+
# bad
248+
if val == None:
227249
...
228250

251+
# good
252+
if val is None:
253+
...
229254
```
230255

231256
### Single Line Conditionals
@@ -282,14 +307,13 @@ Prefer if/else constructs in those cases.
282307

283308
```python
284309
# sinful
285-
x = val if some_condition else other_val if nested_condition else something_else
310+
x = val if some_condition else other_val if nested_condition else something_else
286311

287312
# good
288313
if some_condition:
289314
x = val
290315
else:
291316
x = other_val if nested_condition else something_else
292-
293317
```
294318

295319
### Forgiveness vs Permission
@@ -308,25 +332,25 @@ if some_dict.has_key('a_key'):
308332
else:
309333
...
310334

311-
if val != 0:
312-
10 / val
313-
314335
# good
315336
try:
316337
do_something(some_dict['a_key'])
317338
exception KeyError:
318339
...
319340

341+
# bad
342+
if val != 0:
343+
10 / val
344+
345+
# good
320346
try:
321347
10 / val
322348
except ZeroDivisionError:
323-
...
324-
325-
349+
...
326350
```
327351

328352
## Strings
329-
Do not compare strings with ''is''.
353+
Do not compare strings with `is`.
330354

331355
''is'' has bugs when comparing with strings.
332356

@@ -339,9 +363,9 @@ name == 'bob'
339363
```
340364

341365
### Double vs single Quotes
342-
Prefer double quotes ("b") when your string is multiple words or formatting.
366+
Prefer double quotes (`"hi bob"`) when your string has multiple words or when formatting.
343367

344-
If the string is a single word use single quotes ('b').
368+
If the string is a single word use single quotes (`'bob'`).
345369

346370
### Concatenation
347371

@@ -601,7 +625,7 @@ For example when you don't want to return a collection or alter one.
601625

602626
As seen here the for loop benchmarks as more efficient
603627

604-
{{:devops:pythonportal:test_results.png?200|}}
628+
```{{:devops:pythonportal:test_results.png?200|}}```
605629

606630
```python
607631
def some(x):
@@ -682,28 +706,6 @@ def compare_interests(other_persons_interests):
682706
```
683707

684708
### Tuples
685-
Single element tuples should have a comma after first item
686-
687-
This rule avoids unexpected behavior
688-
```python
689-
# Unexpected behavior
690-
print '{}'.format((9)) # => 9
691-
print '{}'.format((9,)) # => (9,)
692-
693-
# Even worse
694-
x = (9)
695-
y = (9,)
696-
697-
print '{}'.format(x) # => 9
698-
print '{}'.format(y) # => (9,)
699-
700-
def action():
701-
# Assumed to return a tuple with one string item
702-
return ('Some cool string')
703-
704-
print action().__class__ #=> <type 'str'>
705-
706-
```
707709

708710
Don't use implied tuples
709711

@@ -731,6 +733,7 @@ print 'Put', 'spaces', 'between', 'these', 'words'
731733
```
732734

733735
# Imports There are several ways to import a module in python
736+
734737
```python
735738
1) import package.a # Absolute import
736739
2) import package.a as a_mod # Absolute import bound to different name
@@ -741,16 +744,19 @@ print 'Put', 'spaces', 'between', 'these', 'words'
741744

742745
### Do's * Always prefer to use 1st syntax - **Absolute Imports**
743746
* Put all imports in a central module: In __init__.py
747+
744748
```python
745749
from . import a
746750
from . import b
747751
```
748752

749753
It has two major flaws:\\
750754
it forces all the submodules to be imported, even if you're only using one or two, and you still can't look at any of the submodules and quickly see their dependencies at the top, you have to go sifting through functions.
755+
751756
### Don'ts * you shouldn't be using the 4th syntax, since it only works in python2 and runs the risk of clashing with other 3rd party modules.
752757

753758
## Classes
759+
754760
When defining a class who does not inherit give it ''object'' instead of nothing.
755761

756762
Not every method should be accessed outside of the class.
@@ -788,11 +794,13 @@ def add(a,b):
788794
## Functions/Methods
789795

790796
### Closures
797+
791798
User-defined functions incur a layer of overhead that hurts performance.
792799

793800
Therefore, lambdas and nested functions should be avoided (unless it promotes readability).
794801

795802
### Recursion
803+
796804
Python does not optimize for recursion.
797805

798806
The for loop is, effectively, the same abstraction that recursion provides in functional programming languages.
@@ -897,6 +905,7 @@ except Exception:
897905
```
898906

899907
### EAFP
908+
900909
**EAFP** = Easier to ask forgiveness than permission.
901910

902911
As seen in conditions section, exception catching is preferred over checking an object before using.
@@ -906,15 +915,16 @@ Python's performance does not suffer when exception handling.
906915
Be careful not to abuse this rule in your flow control.
907916

908917
## Regular Expressions
909-
Use raw strings (''r'.+' '') for regex values.
918+
919+
Use raw strings (`r'.+'`) for regex values.
910920

911921
Raw strings preserve escape characters.
912922

913923
This will save you from unexpected behavior.
914924

915-
Avoid star wild cards ''\*'', use plus ''+'' instead.
925+
Avoid star wild cards `*`, use plus `+` instead.
916926

917-
Bugs are caused because ''\*'' also allows for zero occurrences.
927+
Bugs are caused because `*` also allows for zero occurrences.
918928

919929
Complex regex should be compiled before using.
920930
the resulting variable's name should clearly define it's purpose.

0 commit comments

Comments
 (0)