[Python-ideas] data structures should have an .any() method

Nick Coghlan ncoghlan at gmail.com
Fri Sep 4 12:03:21 CEST 2009

Stefan Behnel wrote:
> It would therefore be nice to have a common ".any()" method on data
> structures that would just read an arbitrary item from a container.

I'd advise against bare name "any" for this, since we already have the
any() builtin with a completely different meaning. "getany" would
probably be OK though.

I'd also advise against using a method for this, since there is a
reasonable default implementation that can be employed:

def getany(container)
  if container:
    if isinstance(container, collections.Sequence):
      return container[0]
      for x in container:
        return x
  raise ValueError("No items in container")

Finally, I'd suggest that any such function would belong in the
collections module rather than being made a builtin.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list