[Tutor] Some questions about my yen-USD.py
Luke Paireepinart
rabidpoobear at gmail.com
Mon Sep 18 15:49:42 CEST 2006
Dick Moores wrote:
> At 01:00 AM 9/18/2006, Alan Gauld wrote:
>
>>>> 1. in your roundNumber function, you define a function
>>>> incrementDigit.
>>>> I'm pretty sure that this function is destroyed and recreated
>>>> every time
>>>> you call the function roundNumber.
>>>>
>>> I don't understand. What's another way?
>>>
>> def f():
>> def g(): return 42
>> return g()
>>
>> def g(): return 42
>> def f() return g()
>>
Alan meant 'def f(): return g()' on that last line there I think :)
>
> Doesn't seem slow to me. But I take your point.
>
Well, there's the whole Python idiom of 'readability > speed'. It
really doesn't matter how
fast something is if it increases readability (unless the slower speed
really makes a difference,
like that guy's program that used range() instead of xrange() and
crashed the computer!)
However, nested functions are not very common, so for me, they are just
distracting.
As Kent said, it may not be that slow to do this, either, and in your
particular program,
if you choose to use nested functions it shouldn't make any noticeable
speed difference.
If you were going to parse 100,000 text files and change any occurrences
of $xx.xx into
Yen from a given conversion rate, then you'd probably want it to be as
efficient as possible,
but if someone's just converting one value, they're not really going to
notice if it's .001 seconds slower, right?
>>
>>> No, I'll take your advice. But I hate to type underscores,
>>> so is there another style I could use for functions that
>>> would be different from the aRandomVariable style I
>>> like for variables?
>>>
Yeah, underscores are kind of bothersome.
That's what I usually end up using, though.
>>
>>> Function Names
>>>
>>> Function names should be lowercase, with words separated by
>>> underscores as necessary to improve readability.
>>>
>> It may be the official style but in practice its not that widely
>> followed.
>>
Yeah, I haven't seen too much of that going on. Most pieces of Python
code I read have some crazy
syntax that I've never seen before. I learn new things every day :D
-Luke
More information about the Tutor
mailing list