[Python-ideas] Combining test and assignment

alex23 wuwei23 at gmail.com
Tue Jan 24 08:33:16 CET 2012


On Jan 22, 2:18 pm, Eike Hein <s... at eikehein.com> wrote:
> Can anyone think of another way to do an assignment
> and a truth test in the same expression with today's
> tools?

I've seen something similar to this used before:
 class Holder(object):
 def __call__(self, value):
 self.value = value
 return self
 def __eq__(self, val):
 return self.value == val
 __req__ = __eq__
 from random import choice
 some_func = lambda: choice(['a','b','c'])
 holder = Holder()
 if holder(some_func()) == 'a':
 print 'A'
 elif holder == 'b':
 print 'B'
 elif holder == 'c':
 print 'C'
As you can see, it still requires a value to be defined before the
conditional, though :)


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /