Checking the boolean value of a collection

Fredrik Lundh fredrik at pythonware.com
Fri Sep 12 17:11:47 CEST 2008


Marco Bizzarri wrote:

> I would like to make  this available to the whole project. I suspect I
> could put it in the package __init__.py... in that way, the
> __builtins__ namespace should have it... am I right?

the __init__ module for package "foo" defines the contents of the "foo" 
module; it doesn't add anything to the builtin namespace.

Diez made a typo in his post, btw.  To add your own builtins, you should 
add them to the "__builtin__" module (no plural s):

      import __builtin__

      try:
          any
      except NameError:
          def any(iterable):
              for element in iterable:
                  if element:
                      return True
              return False
          __builtin__.any = any

      try:
          all
      except NameError:
          def all(iterable):
              for element in iterable:
                  if not element:
                      return False
              return True
          __builtin__.all = all

The "__builtins__" object is an implementation detail, and shouldn't be 
accessed directly.  And I hope I don't need to point out that adding 
custom builtins nillywilly is a bad idea...

</F>




More information about the Python-list mailing list