default argument values qns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    default argument values qns

    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

  • Tim Chase

    #2
    Re: default argument values qns

    > i have declared a function like this:[color=blue]
    >
    > 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 ??[/color]

    [color=blue][color=green][color=darkred]
    >>> def a(arg1, arg2=0):[/color][/color][/color]
    .... print type(arg2)
    ....[color=blue][color=green][color=darkred]
    >>> a("something" )[/color][/color][/color]
    <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



    Comment

    • Fredrik Lundh

      #3
      Re: default argument values qns

      micklee74@hotma il.com wrote:
      [color=blue]
      > 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 ??[/color]

      because you or someone else is passing in a string as the second argument,
      perhaps?
      [color=blue][color=green][color=darkred]
      >>> def aFunction(arg1, arg2=0):[/color][/color][/color]
      .... print type(arg2)
      ....[color=blue][color=green][color=darkred]
      >>> aFunction(1)[/color][/color][/color]
      <type 'int'>[color=blue][color=green][color=darkred]
      >>> aFunction(1, 2.0)[/color][/color][/color]
      <type 'float'>[color=blue][color=green][color=darkred]
      >>> aFunction(1, "two")[/color][/color][/color]
      <type 'str'>

      </F>



      Comment

      • Alexandre Fayolle

        #4
        Re: default argument values qns

        Le 01-06-2006, micklee74@hotma il.com <micklee74@hotm ail.com> nous disait:[color=blue]
        > 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 ??[/color]

        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:
        [color=blue][color=green][color=darkred]
        >>> def aFunction ( arg1 , arg2 = 0):[/color][/color][/color]
        .... print type(arg2)
        ....[color=blue][color=green][color=darkred]
        >>> aFunction(2)[/color][/color][/color]
        <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

        Comment

        Working...