Loading ...
Sorry, an error occurred while loading the content.

185Re: [python-iter] Digest Number 21

Expand Messages
  • Guido van Rossum
    Mar 14, 2001
      > I agree that having "blah in dict" iterate over/check
      > keys, rather than the values, is the natural behavior.
      > I even think I know how to explain it to newcomers.
      > What I don't know is how to write a polymorphic function
      > to iterate over an arbitrary container, since:
      >
      > def column(x):
      > for i in x:
      > print i, "\t", x[i]
      >
      > doesn't work. Given an arbitrary object 'x', is there
      > some way for me to tell whether its iterator is generating
      > indices or values (or both)?

      There isn't. The question may be meaningless (e.g. if 'x' is a tree).

      > Thanks,
      > Greg
      >
      > p.s. I actually have a lot of code like that above, since
      > I use lists for dense vectors, and dicts for sparse
      > vectors.

      Wrap your different representations in a class that presents a uniform
      interface.

      --Guido van Rossum (home page: http://www.python.org/~guido/)
    • Show all 6 messages in this topic