[Tutor] Questions about Functions

Isaac Hall hall@ouhep1.nhn.ou.edu
Tue Feb 18 16:31:01 2003


Hi Bob,
I *think* I know what you are trying to ask here, but stop me if I am off=
=20
track with what you are thinking....

On Tue, 18 Feb 2003, bob smith wrote:

> Hi.  I=92m learning Python and like it very much so far, but I have som=
e=20
> questions when it comes to functions.
>=20
> Question #1
> --------------
> What=92s wrong with passing a variable to a function that receives it w=
ith the=20
> same name?
>=20
> def func(value):
>   print value
>=20
> # main
> value =3D 10
> func(value)

There should be no problem with this...I tried it in the interpreter, and=
=20
it turns out that there is no problem...here is what I did, just to=20
illustrate.=20
>>>def func(value):
...    print value+1

>>>value=3D10
>>>func(value)
11
>>>print value
10

further we can do this too:
>>>def func(value):
...    value=3Dvalue+1
...    print value

>>>func(value)
11
print value
10

>=20
> If =93value=94 is a good, clear name for my variable, why do I have to =
pick a=20
> new name when I use it in a function?  And if I do use a different vari=
able=20
> name for the parameter, like:
>=20
> def func(val):
>   print val
>=20
> Then, if func() calls another function and passes =93val=94, does this =
second,=20
> receiving function have to have yet another name for the parameter (lik=
e =93v=94=20
> or something)?
>


So it appears to me that the answer to this question is that you can reus=
e=20
the same name over and over and over in different functions.


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

Im really not sure, as I have never considered myself knowledgeable of=20
good programming practices.  For what I do, physics, just making it work=20
is considered good programming.

>=20
> def func():
>   print CONSTANT
>=20
> #main
> CONSTANT =3D 10
> func()
>=20
> Since I can=92t assign a new value to CONSTANT in func(), does this mea=
n that=20
> Python encourages the use of global constants used like this?  It seems=
 like=20
> a good idea to me, (especially if CONSTANT is needed in other functions=
 in=20
> the program) but you hear the mantra =93don=92t use global variables=94=
 so much=20
> that I=92m not sure if this is considered poor programming.
>
Like I said, in my line of work, if this works, and doesnt cause any funk=
y=20
bugs to appear, then it is considered good programming...however others=20
probably differ with that opinion, but again, I only write code because I=
=20
want to get the correct answer to whatever problem I am facing....most=20
other programmers have higher aspirations/motivations when they code....

as far as re-assigning a global variable in a function, that is possible=20
too...sort of...

>>>def func(value):
...    return value+1
>>>value=3D10
>>>value=3Dfunc(value)
>>>print value
11=20
=20
>=20
> Question #3
> --------------
> If you know your program is not going to be imported (only run as a=20
> stand-alone program) do you ever need to consider having this kind of t=
hing:
>=20
> if __name__ =3D=3D =93__main__=94: main()

Only if you ever want to test the program as a stand-alone thing...i.e.=20
before it is ever imported into anything.

Hope this helps

Ike
>=20
>=20
>=20
>=20
>=20
>=20
> _________________________________________________________________
> Protect your PC - get McAfee.com VirusScan Online =20
> http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3D3963
>=20
>=20
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>=20

--=20