3 ms·
There is a bug in #1. The condition should be `i % 2 == 0` to get even numbers: def halves_of_evens(nums): for i in nums: div, mod = divmod(i
by d0mine 16y ago
There is a bug in #1. The condition should be `i % 2 == 0` to get even numbers:
def halves_of_evens(nums):
for i in nums:
div, mod = divmod(i, 2) #NOTE: it might be slower
if mod == 0: #NOTE: `not mod` is not explicit enough
yield div
#2: Function level import is a bad practice though it might be justified in this case.
#3: `users` is not defined. `sqlalchemy` names are not defined. The file should be opened for writing in the json example.
#5 It is simpler to allow the animal to know its kind:
def identify(animal):
return animal.__class__.__name__
or just:
animal.kind
#6 `elem.xpath('./a/@href')` is simpler than
[a.attrib[’href’]
for a in elem.find(’./a’)
if ’href’ in a.attrib]
Also `elem.iterlinks()` could be used
http://codespeak.net/lxml/lxmlhtml.html#working-with-links http://codespeak.net/lxml/lxmlhtml.html#working-with-links
There is no `lxml.CSSSelector` (python-lxml 2.2.6-1). Use `elem.cssselect`
#7 You don't need to add `ifmain` stub in every module:
$ python -mdoctest module_with_doctests.py
$ python -munittest module_with_unittests.py
# 8 & 9: The first variant of `make_counter()` won't work without `nonlocal i` -- it leads to `UnboundLocalError` otherwise.
Despite the fact that it works on my machine on all available Python implementations such code should be avoided:
assert float(’0.20000000000000007’) == 1.1 - 0.9 #XXX horror!
# 10 & 11: Use stdlib's version:
try: import json
except ImportError:
import simplejson as json
It is a matter of preference but it makes testing more predictable.
#13 `__builtins__` is not defined on jython:
try: import __builtin__ as builtins
except ImportError:
import builtins # py3k
- pyre 16y ago> #2: Function level import is a bad practice though > it might be justified in this case. Ah, but: > Special cases aren't special enough to break the rules. ;-)
- siika2000 16y agoWhy is function level import considered bad practice?
- d0mine 16y agoDeadlocking on the import lock might be introduced by an innocent code that uses a library with function level imports. In general globals should be written at a global level.
- dustingetz 16y agohttp://stackoverflow.com/questions/1699108/local-import-statements-in-python http://stackoverflow.com/questions/1699108/local-import-stat...