[Python-ideas] "on" statement

George Sakkis george.sakkis at gmail.com
Thu Apr 1 22:54:53 CEST 2010


On Thu, Apr 1, 2010 at 8:54 PM, Bruce Leban <bruce at leapyear.org> wrote:

> I think this is a great idea, but I would not want to introduce a new
> keyword. How about
>     foo is in bar     = hasattr(foo,bar)
>     foo is not in bar = not hasattr(foo,bar)
> But there are other common checks I do all the time that I would really like
> to have shortcuts for. Here are ways to do them without new keywords:
>     foo is class bar = isinstance(foo,bar)
>     foo is from bar  = issubclass(foo,bar)
>     foo is with bar  = type(foo) == type(bar)
> Can we make an exception to the moratorium, just for today?

I hope so! I'm tossing in a few more suggestions that would make some
idioms more intuitive:

* iterable except for item  := (x for x in iterable if x!=item)

* pass from iterable := for i in iterable: pass

* global return value := sys.exit(value)

* import obj in module as name

   import module
   setattr(module, name, obj)
   del module

* not raise:
      BLOCK

     try: BLOCK
     except: pass

* with x as is:
        BLOCK

    _x = deepcopy(x)
    try:
        BLOCK
    finally:
        x = _x; del _x

*  try for i in iterable:
        BLOCK1
    else:
        BLOCK2

    for i in iterable:
        try: BLOCK1; break
        except: pass
    else:
        BLOCK2


George



More information about the Python-ideas mailing list