default argument values qns

Alexandre Fayolle alf at merlin.fayauffre.org
Thu Jun 1 09:51:23 EDT 2006


Le 01-06-2006, micklee74 at hotmail.com <micklee74 at hotmail.com> nous disait:
> hi
> i have declared a function like this:
>
> def aFunction ( arg1 , arg2 = 0):
>          ....
>          print type(arg2)
>
> when i try to print the type of arg2, it gives me 'str' type..why is it
> not integer type, since i have
> declared it as 0 ??

You probably either called the function with a string as the second
argument, or assigned a string to arg2 in the ... part of the function. 

On my box, I get what you would expect:

>>> def aFunction ( arg1 , arg2 = 0):
...          print type(arg2)
... 
>>> aFunction(2)
<type 'int'>


Now, remember that there are no variables in Python, only identifiers,
which are refering to values. When you print type(arg2), you are not
printing the "type of variable arg2", but the "type of the value
referenced by arg2", which is quite different, especially because it can
change during the execution of the program. 

-- 
Alexandre Fayolle                              LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian:  http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
Python et calcul scientifique:           http://www.logilab.fr/science



More information about the Python-list mailing list