[Tutor] Some questions about my yen-USD.py

Alan Gauld alan.gauld at freenet.co.uk
Mon Sep 18 10:00:27 CEST 2006


>>  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()

The two bits of code do the same thing but the first
constructs/deletes g() each time.

> And what's the downside of the way I've done it?

Its slow...

> How do you keep from causing all that destruction and recreation?

See above

> And what's bad about it?

Its slow

But sometimes defining a function inside another function is
what you want, because you need to limit visibility, or it needs
to access variables that are local to the outer function.
But unless you have a very clear idea of why you want to
define a nested function its better to make them global.

> 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?

Personally I don't differentiate variables and functions
in Python (partly because Python doesn't - they are
all just names) mainly because functions are usually
obvious by dint of the parentheses used to call them.

> 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.

> So I guess I should start learning to type underscores accurately.

Me too, I guess :-)

Alan G. 



More information about the Tutor mailing list