Doing things in one line?

Magnus L. Hetland mlh at vier.idi.ntnu.no
Mon Jan 3 17:33:28 EST 2000


Gerrit Holl <gerrit.holl at pobox.com> writes:

> Hello,
> 
> I know there's a FAQ entry called "Can I write obfuscated one-liners
> in Python?", but for fun, I'm trying to do it now.
> 
> Can I do this in one line:
> if a: foo()
> elif b: bar()
> else: foobar()
> 
> Is it possible with an intelligent construct that looks like:
> a and foo() or a and bar() or foobar()
> 
> This does both foo() and foobar().

Just be sure that all your functions (foo, bar and foobar) return true
(i.e. 1). If they don't (or you're not sure) you must do something
like:

a and (foo() or 1) or b and (bar() or 1) or foobar()

> 
> And what about while- and forloops?

Use lambda and recursion or map/reduce etc.

> 
> regards,
> Gerrit.
> 
> -- 
> -----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
> Version: 3.12
> GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
> Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
> -----END GEEK CODE BLOCK-----
> 

-- 
--

  Magnus
  Lie
  Hetland



More information about the Python-list mailing list