This is the sort of post my wife says doesn’t count when I tell her I wrote a new blog entry.
Anyhow, I’m curious how people format their code. For the stuff below, how would you write it? Feel free to do whatever you want; make temporary variables, rearrange blocks, whatever. I’m looking for interesting new ways to make my code as easy to comprehend as possible.
Here’s an example of how I break assignments over multiple lines:
# By the way, self.categories is a dictionary, not a list,
# because there's no guarantee that all the keys everywhere
# will be contiguous.
cat1, cat2, cat3, cat4, cat5 =
[self.categories[x] for x in range(1, 6)]
This next excerpt shows a couple of my habits. I love the new conditional assignment possible in 2.5, I hate making lots of intermediate variables, and I really love using list comprehensions rather than for loops.
def my_send_methods(self, SendMethod, disabled=False):
return [(x.id, x.display_name,
{
'selected':1 if self.preferred_send_method == x else None,
'disabled':1 if disabled else None,
})
for x in SendMethod.constants.values()]
viewer discretion advised on this next block
This next block is the return statement from one of my TurboGears 1.0 controller methods.
return dict(htmlclass="advancedscheduling", v2org=v2org, name=name,
employees=[(0, 'None')] + [(x.id, x.display_name,
{'selected':1 if x.id == employee_id else None})
for x in v2org.employees],
locations=[(x.id, x.display_name, {'selected':1 if x.id == location_id else None})
for x in v2org.locations],
statuses=[(x.id, x.display_name, {'selected':1 if x.id == status_id else None})
for x in model.ShiftStatus.select()],
)
So, how to make that better? I want to preempt suggestions to do stuff like:
a = [...]
b = [...]
c = [...]
return dict(a=a, b=b, c=c)
That’s not better! For two reasons:
-
You’re taking up way more vertical space
Taken to its logical conclusion, you’ll take a statement like
return dict( employees=[(0, 'None')] + [(x.id, x.display_name, {'selected':1 if x.id == employee_id else None}) for x in v2org.employees] )And then rewrite it as
employees = [(0, 'None')] for emp in v2org.employees: if emp.id == employee_id: d = {'selected': 1} else: d = {'selected':None} t = (emp.id, emp.display_name, d) employees.append(t) return dict(employees=employees)I find this annoying, but not as annoying as the next point.
-
It is no longer obvious that each value is calculated independently
Repeating the example from above:
a = [...] b = [...] c = [...] return dict(a=a, b=b, c=c)
Until I read inside those list comprehensions, or even worse, follow the functions you used to define each one of those, I can’t be certain that each of those variables are determined independently of each other.In other words, in that version, at a casual glance, it is possible that b depends on a. But in the dictionary approach, it is obvious that b does not depend on a, since a doesn’t exist in any way that b can get access to it.
Add New Comment
Viewing 13 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment