[Tutor] Re: [Edu-sig] style question

Kirby Urner urnerk@qwest.net
Thu, 21 Feb 2002 07:18:38 -0800


>
>Personally, I think it's ok to add booleans; I feel it's
>fairly safe to assme that equality testing returns an integer.

OK, and I get the subtle difference now.

I'd likely still go for a list comprehension over a
map-lambda, esp. when teaching newcomers.  Call me
lazy.

And note:  if we're so paranoid as to distrust that ==
might be reliable, given it can be overridden by an
object, so might % be overidden, messing up the filter-
based test as well:

  >>> class T:
         def __init__(self,val):
             self.val = val
         def __eq__(self,other):
             return self.val == other.val
         def __mod__(self,other):
             return 0
         def __repr__(self):
             return str(self.val)


  >>> t0 = T(1)
  >>> t1 = T(45)
  >>> [t0,t1]     # so, no evens, yes?
  [1, 45]
  >>> len(filter(None,[i%2==0 for i in [t0,t1]]))  # think again!
  2

Just can't be too careful. :-D

Kirby