https://youtu.be/qUeud6DvOWI

https://youtu.be/fMRzuwlqfzs

1. use fstring

def manual_str_formatting(name, subscribers):
	if subscribers > 100000:
		print("Wow " + name + "! you have " + str(subscribers) + " subscribers!")
	else: 
		print("Lol " + name + " that's not many subs")
def manual_str_formatting(name, subscribers):
	if subscribers > 100000:
		print (f"Wow {name}! you have {subscribers} subscribers!")
	else: 
		print(f"Lol {name} that's not many subs")

2. use context manager

f.write() 에 error가 생기면 file will never be closed

def manually_calling_close_on_a_file(filename) :
	f = open(filename, "w")
	f.write("hello!\\n")
	f.close()
def manually_calling_close_on_a_file(filename) :
	with open(filename) as f:
		f.write("hello!\\n") N

	# or
	try:
		f = open(filename, "w")
		f.write("hello!\\n")

	finally :
		f.close()

3. using a bare except clause

ctrl-c (keyboardInterrupt) is not a subclass of Exception Class.

만약 그냥 except 만 써놓으면 ctrl-c 로 안꺼짐

def bare_except():
	while True:
		try :
				s = input("Input a number: ")
				x = int(s)
				break
				
		except : # oops! can't CTRL-C to exit
			print("Not a number, try again")
def bare_except():
	while True:
		try :
				s = input("Input a number: ")
				x = int(s)
				break
				
		except ValueError:   # or 	except Exception:
			print("Not a number, try again")

4. don’t use default mutable arguments

Argument defaults are defined (initialized once) when the function is defined (first loaded into the memory), not when it is run.

Default value for my_list will always point back to the same area in memory.

In this case, every call to the function is sharing the same list.

def append(n, my_list=[]):
	my_list.append(n)
	return ln

l1 = append(0) # [0]
l2 = append(1) # [0, 1] oops
def append(n, l=None):
	l = l or []   # same as    l = [] if l is None else l
	l.append(n)
	return ln

l1 = append(0) # [0]
l2 = append(1) # [1]