python newbie

Philip Swartzleonard starx at pacbell.net
Mon Apr 8 07:06:48 EDT 2002


Chris || Sun 07 Apr 2002 10:25:08p:

> In article <3CB106AA.1AA3A5BF at engcorp.com>,
>  Peter Hansen <peter at engcorp.com> wrote:
> 
>> Chris wrote:
>> > 
>> > In article <Xns91E989929996CRASXnewsDFE1 at 130.133.1.4>,
>> >  Philip Swartzleonard <starx at pacbell.net> wrote:
>> > 
>> > > def percent_chance( percentage ):
>> > >       "Assumes percentage as number 0-100"
>> > >       if random.random() > (percentage / 100.0):
>> > >               return 1
>> > >       else:
>> > >               return 0
>> > 
>> > Might more concisely be written as:
>> > 
>> > def percent_chance( percentage ):
>> >      return random.random() > (percentage / 100.0)
>> 
>> But this is Python, and a newbie.  I think Philip's
>> effort was more suited to the audience, as it is
>> more readable. 
> 
> It's redundant, and "random.random() > (percentage / 100.0)" is no
> more readable with a "if" in front of it than it is with a "return".
> 
> I'm not trying to be rude, it just seems silly to do the work twice.

When defining small utility functions (such as these, and things like 
'cap number between two others' and 'calcualte slope', and that usually 
have only a few lines of code no matter how you stretch it ;), i like to 
keep things as open and as mentally simple as possible. And it seems 
like it takes less mental effort to look at my version and see the 
general nature of it than yours; where I have to parse the whole line, 
think about it, and realise that the return value is the result of the 
boolish-returning > operator, and not any of the other stuff on the 
line. I also like the pattern of 'compute somehting, and then act on 
it'... it seems to act in the same way we would if we eg. rolled a 
percentile die, then looked at the chart to see if we passed.

Not tryint to be rude, just a matter of style really. Also, i should go 
to bed before I completly stop functioning. (I should queue a message 
like this and look at it in the morning...... but i never do :\)

O-ya-su-mi
-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list