[Tutor] Functions and Variable Names
alan.gauld@bt.com
alan.gauld@bt.com
Tue Feb 25 05:39:10 2003
> Okay, if I have separate functions, I know I can pass a value
> between them with the same variable name. But is that considered
> poor programming?
No its perfectly cvommon and acceptable practice.
However....
> def f1(score): print "Your score of %d has been recorded." %score
>
> def f2(score):
...
> f1(score)
>
> def main():
> score = 90
> f2(score)
> "score" is the prefect name for what I'm representing, but
> should I have different names for this value in each function?
To distinguish between the global variable and the parameters
you might like to adopt a naming technique. My personal one is
like so:
def f1(aScore): ....
def f2(aScore):....
def main():
theScore = 90
f2(theScore)
Thus the top level one (which gets the initial value) I call theXXX
and the parameters - which could be any value - I call aXXX.
FWIW I also use this technique in class definitions for languages like
Java/C++ where explicit 'self' is not used. In Python that specific
problem doesn't exist.
HTH,
Alan G.