[Tutor] For-else... Any other handy constructs hiding in Python?

Fred Stluka fred at bristle.com
Tue Jun 28 17:51:06 EDT 2016


(Re-sending to list instead of just to Alex.)

Alex,

Are you aware of these?
- try ... finally
- try ... except ... else
- continue
- break
- with
- @decorator
- x = a if b else c
    -- Like the ternary operator a?b:c in other languages
- x = a or b
   -- Assigns a, defaulting to b if a is false, unlike other
        languages that might assign True or False
   -- Short than the equivalent: x = a if a else b

Any other cool tricks out there, anyone?
--Fred
------------------------------------------------------------------------
Fred Stluka -- mailto:fred at bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
------------------------------------------------------------------------

On 6/24/16 9:07 AM, Alex Hall wrote:
> Hey all,
> I was looking at how to make my different jobs retry themselves, and found
> the for-else construct. I know loops, comprehensions, ifs, and the like,
> but I never knew for-else was available. Are there other constructs that I
> may have missed--shortcuts or seemingly odd pairings of control statements
> like this? Python has a lot of really cool shortcuts and features, like
> get/hasattr, the yield keyword, or comprehensions. I always like finding
> more to add to the toolbox.
>



More information about the Tutor mailing list