Talking about naming convention in Python, including topics discussed in “A Python Æsthetic” by Brandon Rhodes, and some personal note.
NOTICE: Credits for example code snippets and various contents in this presentation (exclude the last part, starting from slide 27) fully go to Brandon Rhodes.
13. Relentless Verbs
def database():!
# What does this even do?!
!
def get_database():!
# Gets an instance for me!!
!
def create_database():!
# Creates a database and returns!
# it for me!
14. Relentless Verbs
def database():!
# What does this even do?!
!
def get_database():!
# Gets an instance for me!!
!
def create_database():!
# Creates a database and returns!
# it for me!
22. Problem of Pluralization
>>> connections!
[<SQLConnection at 0xb72ff4d8>,!
<SQLConnection at 0xb72ff4d0>,!
<SQLConnection at 0xb72ff4f0>]!
!
>>> connections!
{'master': <SQLConnection at 0xb72ff4d8>,!
'backup': <SQLConnection at 0xb72ff4f0>}
28. My Problem
def user_profile(request, username):!
user = request.user!
if user.is_authenticated:!
if user.username == username:!
# Show personal page!
else:!
# Show public page!
else:!
# Show page for anonymous user
29. My Problem
def user_profile(request, username):!
user = request.user!
if user.is_authenticated:!
if user.username == username:!
# Show personal page!
else:!
# Show public page!
else:!
# Show page for anonymous user
31. My Problem
def user_profile(request, username):!
user = request.user!
if user.is_authenticated():!
if user.username == username:!
# Show personal page!
else:!
# Show public page!
else:!
# Show page for anonymous user
38. Similarly Problematic
# Makes more sense being a variable!
request.url!
!
# Obviously needs processing. Okay.!
document.md5()!
!
# ???!
rectangle.left!
rectangle.center
39. No Perfect Solution
# Parentheses do not matter in Ruby!
user.authenticated?!
!
# But things can get funky quickly!
proc_obj = proc { user.authenticated? }!
proc_obj.call!
!
# Python can be inconvenient!
user.is_authenticated()!
!
# But obvious things stay obvious!
func = user.is_authenticated!
func()