22
loading...
This website collects cookies to deliver better user experience
# very bad practice
try:
do_something()
except:
pass
# A slightly better way(included logging), but still a bad practice:
try:
do_something()
except Exception:
logging.exception() # although, logging is a good practice
# When we don't use `Exception` we will also be catching events like system exit.
# So, using Exception means that we are only catching Exceptions.
# A better way:
try:
do_something()
except ValueError:
logging.exception()
# some code to handle your exception gracefully if required
ValueError
. # using camelCase is not a convention in python
def isEmpty(sampleArr):
...
# instead use snake_case in python
def is_empty(sample_arr):
...
snake_case
is preferred for variables, functions and method names. However, for classes, PascalCase
is used.# Don't do this:
if 0 < x and x < 10:
print('x is greater than 0 but less than 10')
# Instead, do this:
if 0 < x < 10:
print('x is greater than 0 but less than 10')
# wrong way!
# a sure shot way to get some unintended bugs in your code
def add_fruit(fruit, box=[]):
box.append(fruit)
return box
# correct way!
# recommended way for handling mutable default arguments:
def add_fruit(fruit, box=None):
if box is None:
box = []
box.append(fruit)
return box
# Avoid using it
# %-formatting
name = "James Bond"
profession = "Secret Agent"
print("Hello, %s. You are a %s." % (name, profession))
# slightly better
# str.format()
print("Hello, {}. You are a {}.".format(name, profession))
# Short, crisp and faster!
# f-strings
print(f"Hello, {name}. You are a {profession}.")
# Filename: run.py
if __name__ == '__main__':
print('Hello from script!')
$ python run.py
$ Hello from script!
Hello from script!
will not be printed if the module is imported into any other module.if x < 10:
return 1
else:
return 2
return 1 if x < 10 else 2
list_of_fruits = ["apple", "pear", "orange"]
# bad practice
for i in range(len(list_of_fruits)):
fruit = list_of_fruits[i]
process_fruit(fruit)
# good practice
for fruit in list_of_fruits:
process_fruit(fruit)
# Don't do this:
index = 0
for value in collection:
print(index, value)
index += 1
# Nor this:
for index in range(len(collection)):
value = collection[index]
print(index, value)
# Definitely don't do this:
index = 0
while index < len(collection):
value = collection[index]
print(index, value)
index += 1
# Instead, use `enumerate()`
for index, value in enumerate(collection):
print(index, value)
d = {"foo": 1}
# bad practice
f = open("./data.csv", "wb")
f.write("some data")
v = d["bar"] # KeyError
# f.close() never executes which leads to memory issues
f.close()
# good practice
with open("./data.csv", "wb") as f:
f.write("some data")
v = d["bar"]
# python still executes f.close() even if the KeyError exception occurs
s = set(['s', 'p', 'a', 'm'])
l = ['s', 'p', 'a', 'm']
# ok for small no. of elements
def lookup_list(l):
return 's' in l # O(n)
# better for large no. of elements
def lookup_set(s):
return 's' in s # O(1)
# bad practice
from math import *
x = ceil(x)
# good practice
from math import ceil
x = ceil(x) # we know where ceil comes from
d = {
"name": "Aarya",
"age": 13
}
# Dont do this
for key in d:
print(f"{key} = {d[key]}")
# Instead do this
for key,val in d.items():
print(f"{key} = {val}")