[Tutor] "if clause" in list comprehensions.

Douglas Philips dgou at mac.com
Mon Oct 19 22:10:50 CEST 2009


On or about 2009 Oct 19, at 3:57 PM, Sander Sweers indited:
> I missed that the try: did not return anything. I was thinking more of
> something like this.
>
> def upperfy(item):
>    try:
>        item.upper()
>        return item
>    except AttributeError:
>        return item
>
> Thanks for correcting me!

Depending on what 'item' is, item.upper() might return an new thing,  
but the code looks as if you're expecting .upper() to modify item  
itself. If item is a string, item.upper() will return a new string and  
leave item alone (since strings are immutable in Python).

-Doug


More information about the Tutor mailing list