Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update examples to Python 3 in code style section #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions docs/writing/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code.

.. code-block:: python

print 'one'; print 'two'
print('one'); print('two')

if x == 1: print 'one'
if x == 1: print('one')

if <complex comparison> and <other complex comparison>:
# do something
Expand All @@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code.

.. code-block:: python

print 'one'
print 'two'
print('one')
print('two')

if x == 1:
print 'one'
print('one')

cond1 = <complex comparison>
cond2 = <other complex comparison>
Expand Down Expand Up @@ -357,9 +357,7 @@ Instead, use a list comprehension:

.. code-block:: python

four_lists = [[] for __ in xrange(4)]

Note: Use range() instead of xrange() in Python 3.
four_lists = [[] for __ in range(4)]

Create a string from a list
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -584,26 +582,26 @@ list of what is considered false.
.. code-block:: python

if attr == True:
print 'True!'
print('True!')

if attr == None:
print 'attr is None!'
print('attr is None!')

**Good**:

.. code-block:: python

# Just check the value
if attr:
print 'attr is truthy!'
print('attr is truthy!')

# or check for the opposite
if not attr:
print 'attr is falsey!'
print('attr is falsey!')

# or, since None is considered false, explicitly check for it
if attr is None:
print 'attr is None!'
print('attr is None!')

Access a Dictionary Element
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -617,22 +615,22 @@ or pass a default argument to :py:meth:`dict.get`.

d = {'hello': 'world'}
if d.has_key('hello'):
print d['hello'] # prints 'world'
print(d['hello']) # prints 'world'
else:
print 'default_value'
print('default_value')

**Good**:

.. code-block:: python

d = {'hello': 'world'}

print d.get('hello', 'default_value') # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value'
print(d.get('hello', 'default_value')) # prints 'world'
print(d.get('thingy', 'default_value')) # prints 'default_value'

# Or:
if 'hello' in d:
print d['hello']
print(d['hello'])

Short Ways to Manipulate Lists
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -783,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list.

a = [3, 4, 5]
for i, item in enumerate(a):
print i, item
print(i, item)
# prints
# 0 3
# 1 4
Expand All @@ -804,7 +802,7 @@ files for you.

f = open('file.txt')
a = f.read()
print a
print(a)
f.close()

**Good**:
Expand All @@ -813,7 +811,7 @@ files for you.

with open('file.txt') as f:
for line in f:
print line
print(line)

The ``with`` statement is better because it will ensure you always close the
file, even if an exception is raised inside the ``with`` block.
Expand Down