News for hackers. Stuff that matters.
Re:The Zen of Python by Example by d0mine on Thursday February 10, 2011 @06:09PM (#2204267)
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

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
Re:The Zen of Python by Example by siika2000 on Friday February 11, 2011 @02:19AM (#2205482)
Why is function level import considered bad practice?
Re:The Zen of Python by Example by d0mine on Friday February 11, 2011 @08:11AM (#2206175)
Deadlocking 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.

Re:The Zen of Python by Example by dustingetz on Friday February 11, 2011 @11:20AM (#2206844)
Re:The Zen of Python by Example by pyre on Thursday February 10, 2011 @08:33PM (#2204756)

  > #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.
;-)