Functions Not Fun (yet)-please help!

Emile van Sebille emile at fenx.com
Sun Jan 16 10:24:14 EST 2011


On 1/16/2011 6:49 AM Cathy James said...
> Dear all,
>
> I can't thank you enough for taking time from your busy schedules to assist
> me (and others) in my baby steps with Python. Learning about functions now
> and wondering about some things commented in my code below. Maybe someone
> can break it down for me and show me why i cant print the function i
> created. I am using IDLE, saved it as .py
>
> def my_func(a, b="b is a default" ,c="c is another default"):
>      print (a)
>      print (b)
>      print (c)
>
>
> #printing the function itself:
>
> #1. assign value to a only, b and c as default:
> a= "testing"
> print (my_func(a,b,c)) #why does program say c is not defined, tho default
> in function above?

Because c is not defined in the scope outside the function where you are 
passing the variables into my_func. To see the effect of the defaults, 
call with only 'a', as otherwise any variables you pass in must have 
been previously defined.

 >>> print (my_func(a))
testing
b is a default
c is another default
None
 >>>

Emile

> #2. assign a and b only, c is default
> print my_func(a="testing a", b="testing b")
>
>





More information about the Python-list mailing list