This link has been bookmarked by 187 people . It was first bookmarked on 22 Jun 2006, by craig hancock.
-
25 Aug 15
-
13 Aug 11
-
Use function factories to create utility functions. Often, especially if you're using map and filter a lot, you need utility functions that convert other functions or methods to taking a single parameter. In particular, you often want to bind some data to the function once, and then apply it repeatedly to different objects.
-
-
10 Jul 11
-
21 Jun 11
-
14 Jun 11
-
13 Jun 11
-
12 Jun 11
-
01 Oct 10
-
28 Jun 10
-
10 Jun 10
-
19 Apr 10
-
30 Mar 10
-
08 Dec 09
-
11 Nov 09
-
08 Oct 09
-
30 Sep 09
-
12 Jun 09
-
02 Jun 09
-
31 May 09
-
27 May 09
-
23 May 09
-
26 Apr 09
makoto tsuyukiPythonのイディオム
What idioms should I use to make my code easier to read?
Read "The Python Cookbook", especially the first few chapters. It's a great source of well-written Python code examples. -
13 Mar 09
-
16 Feb 09
-
result = ''.join(strings)
-
for key in d: print key #also works for arbitrary sequence
-
if key not in d: d[key] = []
-
Note: you still need to use d.keys() if you want to mutate the dictionary. for key in d: del d[key] will raise RuntimeError: dictionary changed size during iteration. Use for key in d.keys(): del d[key] instead.
-
use s.startswith('abc') rather than startswith(s, 'abc')
-
Use for line in infile, not for line in infile.readlines()
-
Worse: #check whether int conversion will raise an error if not isinstance(s, str) or not s.isdigit: return None elif len(s) > 10: #too many digits for int conversion return None else: return int(str) Better: try: return int(str) except (TypeError, ValueError, OverflowError): #int conversion failed return None
-
Build strings as a list and use ''.join at the end
-
Wrong: for s in strings: result += s Right: result = ''.join(strings)
-
Use tests for object identity when appropriate: if x is not None rather than if x != None
-
Worse: strings = [] for d in data: strings.append(str(d)) Better: strings = map(str, data)
-
Use list comprehensions where there are conditions attached, or where the functions are methods or take more than one parameter
-
Use function factories to create utility functions
-
def multiply_by_field(fieldname, multiplier): """Returns function that multiplies field "fieldname" by multiplier.""" def multiplier(x): return getattr(x, fieldname) * multiplier return multiplier triple = multiply_by_field('Count', 3) quadruple = multiply_by_field('Count', 4) halve_sum = multiply_by_field('Sum', 0.5)
-
Use the operator module and reduce to get sums, products, etc
-
Worse: sum = 0 for d in data: sum += d product = 1 for d in data: product *= d Better: from operator import add, mul sum = reduce(add, data) product = reduce(mul, data)
-
-
12 Jan 09
-
03 Jan 09
-
09 Dec 08
-
24 Nov 08
-
05 Sep 08
-
25 Aug 08
-
06 Jul 08
-
05 Jul 08
-
30 Jun 08
-
22 Jun 08
-
19 Jun 08
-
17 Jun 08
-
02 Jun 08
-
21 Apr 08
-
04 Apr 08
-
12 Mar 08
-
29 Feb 08
-
02 Feb 08
zarakin nikaraz02-2008: Table of Contents -
- What idioms should I use to make my code easier to read?
- What techniques should I use to make my code run faster?
- Back to the coding guidelines -
18 Jan 08
-
17 Jan 08
-
17 Dec 07
-
10 Dec 07
-
What idioms should I use to make my code easier to read?
Read "The Python Cookbook", especially the first few chapters. It's a great source of well-written Python code examples.
-
Use function factories to create utility functions. Often, especially if you're using map and filter a lot, you need utility functions that convert other functions or methods to taking a single parameter. In particular, you often want to bind some data to the function once, and then apply it repeatedly to different objects. In the above example, we needed a function that multiplied a particular field of an object by 3, but what we really want is a factory that's able to return for any field name and amount a multiplier function in that family:
-
Use zip and dict to map fields to names. zip turns a pair of sequences into a list of tuples containing the first, second, etc. values from each sequence. For example, zip('abc', [1,2,3]) == [('a',1),('b',2),('c',3)]. You can use this to save a lot of typing when you have fields in a known order that you want to map to names:
-
-
16 Oct 07
-
19 Sep 07
-
30 Jul 07
-
15 Jul 07
-
03 Jul 07
-
28 Jun 07
-
31 May 07
-
08 Apr 07
-
20 Mar 07
-
11 Mar 07
-
06 Mar 07
-
20 Feb 07
-
09 Feb 07
-
30 Jan 07
-
27 Dec 06
-
30 Nov 06
-
31 Oct 06
-
24 Oct 06
-
19 Oct 06
-
08 Aug 06
-
07 Aug 06
-
09 Jul 06
-
24 Apr 06
-
21 Apr 06
-
18 Apr 06
-
11 Apr 06
-
07 Apr 06
-
31 Mar 06
-
30 Mar 06
-
20 Jan 06
-
17 Jan 06
-
26 Dec 05
-
20 Dec 05
-
28 Nov 05
-
27 Oct 05
-
20 Oct 05
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.