default argument values qns

M

micklee74

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 ??
thanks
 
F

Fredrik Lundh

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 ??

because you or someone else is passing in a string as the second argument,
perhaps?
.... print type(arg2)
....<type 'str'>

</F>
 
A

Alexandre Fayolle

Le 01-06-2006 said:
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.

.... print type(arg2)
.... <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.
 
T

Tim Chase

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 ??
.... print type(arg2)
....<type 'int'>


Looks like it's an int to me. You must be doing something
spurious in your mysterious "...." that changes the data type.

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top