[Tutor] Questions about Functions

bob smith bobsmith327@hotmail.com
Tue Feb 18 15:43:02 2003


Hi.  I’m learning Python and like it very much so far, but I have some 
questions when it comes to functions.

Question #1
--------------
What’s wrong with passing a variable to a function that receives it with the 
same name?

def func(value):
  print value

# main
value = 10
func(value)

If “value” is a good, clear name for my variable, why do I have to pick a 
new name when I use it in a function?  And if I do use a different variable 
name for the parameter, like:

def func(val):
  print val

Then, if func() calls another function and passes “val”, does this second, 
receiving function have to have yet another name for the parameter (like “v” 
or something)?


Question #2
--------------
Is it considered bad programming to access (and only access) global 
variables in functions, thus treating them as global constants?

def func():
  print CONSTANT

#main
CONSTANT = 10
func()

Since I can’t assign a new value to CONSTANT in func(), does this mean that 
Python encourages the use of global constants used like this?  It seems like 
a good idea to me, (especially if CONSTANT is needed in other functions in 
the program) but you hear the mantra “don’t use global variables” so much 
that I’m not sure if this is considered poor programming.


Question #3
--------------
If you know your program is not going to be imported (only run as a 
stand-alone program) do you ever need to consider having this kind of thing:

if __name__ == “__main__”: main()






_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online  
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963