Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alihalabyah/78aca0fc366429414d72 to your computer and use it in GitHub Desktop.
Save alihalabyah/78aca0fc366429414d72 to your computer and use it in GitHub Desktop.

Writing better python code


Swapping variables

Bad code

>>>a = 23
>>>b = 12
>>>temp = a
>>>a = b
>>>b = temp
>>>print a, b
12, 23

Pythonic

!python
>>>a, b = 23, 12
>>>a, b = b, a
>>>print a, b
12, 23

Counting Condition

Bad code

!python

>>>super_users = get_all_superusers_for_organization(org_name)
>>>if len(super_users) > 0:
>>>    print(u"Super users are {}".format(super_users))
>>>else:
>>>    print(u"No super users")

Pythonic

!python

>>>super_users = get_all_superusers_for_organization(org_name)
>>>if super_users:
>>>    print(u"Super users are {}".format(super_users))
>>>else:
>>>    print(u"No super users")

Accessing index of list

Bad code

!python

>>>langs = ["Python", "go", "lua"]
>>>index = 0
>>>length = len(langs)
>>>while index < length:
>>>    print(index, langs[index])
>>>    index += 1

Pythonic

!python

>>>langs = ["Python", "go", "lua"]
>>>for index, value in enumerate(langs):
>>>    print(index, value)

Check for one condition

Bad code

!python

>>>if user.is_superadmin or user.is_siteadmin or user.is_staff:
>>>    # Grant Access to some page

Pythonic

!python
>>>if any([user.is_superadmin, user.is_siteadmin, user.is_staff]):
>>>    # Grant Access to some page

Check for all condition

Bad code

!python

>>>if user.is_superadmin and user.is_siteadmin and user.is_staff:
>>>    # Grant Access to admin page

Pythonic

!python
>>>if all([user.is_superadmin, user.is_siteadmin, user.is_staff]):
>>>    # Grant Access to admin page

Range check

Bad code

!python

>>>status_code = requests.post("someurl", params={"foo": "bar"}).status_code
>>>if status_code >= 200 and status_code <= 299:
>>>    print("HTTP call is success")

Pythonic

!python

>>>status_code = request.post("someurl", params={"foo": "bar"}).status_code
>>>if 200 <= status_code <= 299:
>>>    print("HTTP call is success")

Is operator

Bad code

!python
>>>250 + 6 is 256
True

>>>250 + 7 is 257
False

Pythonic code

!python
>>>250 + 6 == 256
True

>>>250 + 7 == 257
True

Copy a list

Bad code

!python

>>>langs = ["Python", "Go", "Lua"]
>>>new_langs = langs
>>>new_langs.append("Javascript")
>>>print(langs, new_langs)
(['Python', 'Go', 'Lua', 'Javascript'], ['Python', 'Go', 'Lua', 'Javascript'])

Pythonic

!python
>>>langs = ["Python", "Go", "Lua"]
>>>new_langs = langs[:]
>>>new_langs.append("Javascript")
>>>print(langs, new_langs)
(['Python', 'Go', 'Lua'], ['Python', 'Go', 'Lua', 'Javascript'])

List comprehension

Bad code

!python

>>>nos = [1, 4, 5, 6]
>>>evens = []
>>>for no in nos:
>>>    if no % 2 == 0:
>>>        evens.append(no)
>>>print(evens)
[4, 6]

Pythonic

!python

>>>nos = [1, 4, 5, 6]
>>>evens = [no for no in nos if no % 2 == 0]
>>>print(evens)
[4, 6]

Function defaults

Bad code

!python

>>>def foo():
>>>    return "foo"

>>>def bar(names=[]):
>>>    names.append(foo())
>>>    return names

>>>bar()
["foo"]
>>>bar()
["foo", "foo"]

Function defaults ...

Pythonic

!python

>>>def bar(names=None):
>>>   name = foo()
>>>   if not names:
>>>       return [name]
>>>   names.append(name)
>>>   return names

>>>bar()
['foo']
>>>bar()
['foo']

Get a element in a dictionary

Bad code

!python

>>>d = {'foo': 'bar'}
>>>if 'baz' in d:
>>>    temp = d['baz']
>>>else:
>>>    temp = ""

Pythonic

!python

>>>d = {'foo': 'bar'}
>>>temp = d.get('baz', "")

Frequency count

Bad code

!python

>>>words = ["a", "the", "foo", "bar", "foo"]
>>>frequency = {}
>>>for word in words:
>>>    if word in frequency:
>>>        frequency[word] += 1
>>>    else:
>>>        frequency[word] = 1
>>>print(frequency)
{'a': 1, 'the': 1, 'foo': 2, 'bar': 1}

Frequency count ...

Pythonic

!python

>>>from collections import Counter
>>>frequency = Counter(words)
>>>print(frequency)
Counter({'foo': 2, 'a': 1, 'the': 1, 'bar': 1})

Raising KeyError in dictionary

Bad code

!python

#Look before you leap
>>>d = {'foo': 'bar'}
>>>if 'baz' in d:
>>>    temp = d['baz']
>>>else:
>>>    raise KeyError("baz is missing")

Pythonic

!python

#Easier to Ask for Forgiveness than Permission
>>>try:
>>>    temp = d["baz"]
>>>except KeyError:
>>>    raise KeyError("baz is missing")

Context manager

Bad code

!python

>>>f = open('foo.txt')
>>>print(f.read())
>>>f.close()

Pythonic

!python

>>>with open('foo.txt') as f:
>>>    print(f.read())

Iterating over a file

Bad code

!python

>>>with open('foo.txt') as f:
>>>    for line in f.readlines():
>>>        print(line)

Pythonic

!python

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

dot accessors

Bad code

!python

>>>class Person(object):
>>>    def __init__(self, name, email):
>>>        self.name = name
>>>        self.email = email
>>>p = Person("kracekumar", "me@kracekumar.com")
>>>print(p.name, p.email)
('kracekumar', 'me@kracekumar.com')

dot accessors ...

Pythonic

!python

>>>from collections import namedtuple
>>>Person = namedtuple('Person', ['name', 'email'])
>>>p = Person("kracekumar", "me@kracekumar.com")
>>>print(p.name, p.email)
('kracekumar', 'me@kracekumar.com')

Exceptions

Bad code

!python

>>>try:
>>>   foo()
>>>except:
>>>   print("something") # sometimes people write pass. PURE EVIL !

Pythonic

!python

>>>try:
>>>   foo()
>>>except (IndexError, KeyError) as e:
>>>    print("something went wrong")

Q & A


Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment