Skip to content

Instantly share code, notes, and snippets.

@barnabee
Created August 16, 2020 12:50
Show Gist options
  • Save barnabee/cbf39c9efec69be0e1f0f2bbd8a5407e to your computer and use it in GitHub Desktop.
Save barnabee/cbf39c9efec69be0e1f0f2bbd8a5407e to your computer and use it in GitHub Desktop.
#### Python should have indent-based alternatives to wrapping in all types of bracket
#
# Benefits:
# - indentation means no brackets to match n levels deep
# - new lines can replace commas, easy to reorder
# - feels nice and Pythony, works with the rest of the syntax
# - looks much cleaner and more readable without closing brackets and commas
#
# Instead of this
argparse.add_argument(
'-c', '--config',
default='config.json',
help='use the specified JSON config file')
# Do something like this (note multiple items on a line with commas)
argparse.add_argument(:)
'-c', '--config'
default='config.json'
help='use the specified JSON config file'
# What about dicts?
data = {:}
'name': 'Homer'
'address': {:}
'line1': '724 Evergreen Terrace'
'town': 'Springfield'
'age': 64
# Or using the constructor
data = dict(:)
name='Homer'
address=dict(:)
line1='724 Evergreen Terrace'
town='Springfield'
age=64
# Could be used for lists
boards = [:]
[:]
['X', 'O', 'X']
['O', 'O', 'X']
['O', 'X', 'O']
[:]
['X', 'O', 'X']
['O', 'X', 'O']
['O', 'O', 'X']
# And list comprehensions!
greetings = [:]
f'Hello, {person.name}!'
for person in universe.people.values()
if person.is_awesome and
person.place = 'world'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment