if expression source format

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Dec 11 03:14:45 EST 2008


On Thu, 11 Dec 2008 02:07:54 -0500, Lambert, David W (S&T) wrote:

> The "if" expression leads to long statements.  Please offer suggestions
> to beautify this function.  For this example use maximum line length
> marked by the ####'s.
> 
> Thank you.
> 
> 
> ############################################################## 
> def compute_wind_chill_temperture(T:'Temperature, dF',s:'Wind speed,
> mph')->'dF':
>     '''
>         <http://www.erh.noaa.gov/er/iln/tables.htm#heat index>
>     '''
>     return 35.74 + 0.6215*T + s**0.16*(T*0.4275-35.75) if T < 40 else T

Firstly, you don't need "compute". Every function computes.

Secondly, fix the spelling of temperature.

Thirdly, we can make it easier to read by spreading it out.

############################################################## 
def wind_chill_temperature(
    T:'Temperature, dF',s:'Wind speed, mph')->'dF':
    'http://www.erh.noaa.gov/er/iln/tables.htm#heat index'
    if T < 40:
        return 35.74 + 0.6215*T + s**0.16*(T*0.4275-35.75)
    else: return T


There is no shortage of newlines. They are a renewable resource, don't be 
afraid to use a couple of extra lines to make your code more readable.



-- 
Steven



More information about the Python-list mailing list