Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f15af06

Browse files
authored
Merge pull request #1094 from matheusfelipeog/feat/py2-to-py3-in-code-style-section
Update examples to Python 3 in code style section
2 parents 25a2c4e + 521d8e8 commit f15af06

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

‎docs/writing/style.rst‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code.
6565

6666
.. code-block:: python
6767
68-
print'one'; print'two'
68+
print('one'); print('two')
6969
70-
if x == 1: print'one'
70+
if x == 1: print('one')
7171
7272
if <complex comparison> and <other complex comparison>:
7373
# do something
@@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code.
7676

7777
.. code-block:: python
7878
79-
print'one'
80-
print'two'
79+
print('one')
80+
print('two')
8181
8282
if x == 1:
83-
print'one'
83+
print('one')
8484
8585
cond1 = <complex comparison>
8686
cond2 = <other complex comparison>
@@ -357,9 +357,7 @@ Instead, use a list comprehension:
357357

358358
.. code-block:: python
359359
360-
four_lists = [[] for __ in xrange(4)]
361-
362-
Note: Use range() instead of xrange() in Python 3.
360+
four_lists = [[] for __ in range(4)]
363361
364362
Create a string from a list
365363
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -584,26 +582,26 @@ list of what is considered false.
584582
.. code-block:: python
585583
586584
if attr == True:
587-
print'True!'
585+
print('True!')
588586
589587
if attr == None:
590-
print'attr is None!'
588+
print('attr is None!')
591589
592590
**Good**:
593591

594592
.. code-block:: python
595593
596594
# Just check the value
597595
if attr:
598-
print'attr is truthy!'
596+
print('attr is truthy!')
599597
600598
# or check for the opposite
601599
if not attr:
602-
print'attr is falsey!'
600+
print('attr is falsey!')
603601
604602
# or, since None is considered false, explicitly check for it
605603
if attr is None:
606-
print'attr is None!'
604+
print('attr is None!')
607605
608606
Access a Dictionary Element
609607
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -617,22 +615,22 @@ or pass a default argument to :py:meth:`dict.get`.
617615
618616
d = {'hello': 'world'}
619617
if d.has_key('hello'):
620-
printd['hello'] # prints 'world'
618+
print(d['hello']) # prints 'world'
621619
else:
622-
print'default_value'
620+
print('default_value')
623621
624622
**Good**:
625623

626624
.. code-block:: python
627625
628626
d = {'hello': 'world'}
629627
630-
printd.get('hello', 'default_value') # prints 'world'
631-
printd.get('thingy', 'default_value') # prints 'default_value'
628+
print(d.get('hello', 'default_value')) # prints 'world'
629+
print(d.get('thingy', 'default_value')) # prints 'default_value'
632630
633631
# Or:
634632
if 'hello' in d:
635-
printd['hello']
633+
print(d['hello'])
636634
637635
Short Ways to Manipulate Lists
638636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -783,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.
783781
784782
a = [3, 4, 5]
785783
for i, item in enumerate(a):
786-
printi, item
784+
print(i, item)
787785
# prints
788786
# 0 3
789787
# 1 4
@@ -804,7 +802,7 @@ files for you.
804802
805803
f = open('file.txt')
806804
a = f.read()
807-
print a
805+
print(a)
808806
f.close()
809807
810808
**Good**:
@@ -813,7 +811,7 @@ files for you.
813811
814812
with open('file.txt') as f:
815813
for line in f:
816-
printline
814+
print(line)
817815
818816
The ``with`` statement is better because it will ensure you always close the
819817
file, even if an exception is raised inside the ``with`` block.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /