[Python-ideas] except expression

Chris Angelico rosuav at gmail.com
Sun Feb 16 01:06:50 CET 2014


On Sun, Feb 16, 2014 at 10:37 AM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Steven D'Aprano wrote:
>>
>> On Sat, Feb 15, 2014 at 11:20:13AM +1300, Greg Ewing wrote:
>
>
>>> Also it might be useful to be able to say
>>>
>>>   things.remove(i) (except ValueError: pass)
>>>
>>> which would be equivalent to
>>>
>>>   things.remove(i) (except ValueError: None)
>>
>>
>> Certainly not! pass implies that *no return result is generated at all*,
>> which is not possible in Python.
>
>
> Well, it is, kind of -- an implicit None is produced
> when you don't specify a return value for a function;
> this is a similar thing.
>
> Would it help if it were *only* allow it in a context
> where the value of the expression is going to be ignored?

That's nothing to do with the 'pass' keyword.

def foo():
    print("Hello!")

This will return None, and it doesn't have 'pass'.

def foo(x):
    if x: pass
    return 42

This will not, regardless of the value of x. Python's 'pass' means 'do
nothing', in a place where you contextually need to do something.

ChrisA


More information about the Python-ideas mailing list