Implicit conversion to boolean in if and while statements

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Fri Feb 8 06:11:20 EST 2013


Am 08.02.2013 07:29 schrieb Rick Johnson:

> Consider this:
>
>      if connect("my:db") as db:
>          <do something>
>
> No need to make a call and then test for the validity of the call when you can do both simultaneously AND intuitively.

Would be great, but can be emulated with

def ifiter(x):
     if x: yield x

for db in ifiter(connect("my:db")):
     <do sth with db>

Is not very intuitive, however, but does its job.


Thomas




More information about the Python-list mailing list