What value should be passed to make a function use the default argument value?

Discussion in 'Python' started by LaundroMat, Oct 3, 2006.

  1. LaundroMat

    LaundroMat Guest

    Suppose I have this function:

    def f(var=1):
    return var*2

    What value do I have to pass to f() if I want it to evaluate var to 1?
    I know that f() will return 2, but what if I absolutely want to pass a
    value to f()? "None" doesn't seem to work..

    Thanks in advance.
    LaundroMat, Oct 3, 2006
    #1
    1. Advertising

  2. LaundroMat <> wrote:
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..


    I don't know if I understand correctly here but:

    def f(v=1):
    return v * 2

    f() returns 2
    f(1) returns 2 too

    v = 1
    f(v) returns 2 too

    What do you mean?

    --
    Lawrence - http://www.oluyede.org/blog
    "Nothing is more dangerous than an idea
    if it's the only one you have" - E. A. Chartier
    Lawrence Oluyede, Oct 3, 2006
    #2
    1. Advertising

  3. Re: What value should be passed to make a function use the defaultargument value?

    LaundroMat wrote:

    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()?


    f(1)

    > "None" doesn't seem to work..


    None is an object.

    </F>
    Fredrik Lundh, Oct 3, 2006
    #3
  4. LaundroMat

    Tim Chase Guest

    Re: What value should be passed to make a function use the defaultargument value?

    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..


    >>> def f(var=1):

    .... return var*2
    ....
    >>> f()

    2
    >>> f(0.5)

    1.0
    >>> f(123)

    246
    >>> f('hello')

    'hellohello'


    I'm not sure I follow your problem...what's stopping you from
    passing a value?

    -tkc
    Tim Chase, Oct 3, 2006
    #4
  5. LaundroMat

    Larry Bates Guest

    Re: What value should be passed to make a function use the defaultargument value?

    LaundroMat wrote:
    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..
    >
    > Thanks in advance.
    >

    The answer is don't pass any value.

    print f()

    will print 2

    -Larry Bates
    Larry Bates, Oct 3, 2006
    #5
  6. Re: What value should be passed to make a function use the defaultargument value?

    On Tue, 03 Oct 2006 13:16:57 -0700, "LaundroMat" <> let
    this slip:

    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..
    >
    > Thanks in advance.


    a) if you feel that your program needs to pass a value, fix the program.

    b)
    >>> def f(v=1):

    .... return v*2
    ....
    >>> f()

    2
    >>> f(1)

    2
    >>> f(*[1])

    2
    >>> f(*[])

    2
    >>>


    *
    • is the reverse of def f(*args)

      --
      Thomas Jollans alias free-zombie
    Thomas Jollans, Oct 3, 2006
    #6
  7. LaundroMat

    Stan Graves Guest

    LaundroMat wrote:
    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..



    What about this?

    >>> def f(var=None):

    .... if var == None:
    .... var = 1
    .... return 2*var
    ....
    >>> f()

    2
    >>> f(3)

    6
    >>> a=4
    >>> f(a)

    8
    >>> b=None
    >>> f(b)

    2
    >>>


    --Stan Graves
    Stan Graves, Oct 3, 2006
    #7
  8. Re: What value should be passed to make a function use the defaultargument value?

    LaundroMat a écrit :
    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    >
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..


    Have you tried f(1) ?
    Bruno Desthuilliers, Oct 3, 2006
    #8
  9. LaundroMat wrote:
    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..


    If you *absolutely* want to pass a value and you don't know the default
    value (otherwise you could just pass it):

    >>> import inspect
    >>> v = inspect.getargspec(f)[3][0] # first default value
    >>> f(v)

    2
    Rob De Almeida, Oct 3, 2006
    #9
  10. On 2006-10-03, LaundroMat <> wrote:
    > Suppose I have this function:
    >
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..
    >
    > Thanks in advance.


    One possible way to do what I think you want is to code as follows:

    class Default (object):
    pass

    def f(var=Default):
    if var is Default:
    var = 1
    return var * 2

    --
    Antoon Pardon
    Antoon Pardon, Oct 4, 2006
    #10
  11. LaundroMat

    LaundroMat Guest

    Rob De Almeida wrote:

    > LaundroMat wrote:
    > > Suppose I have this function:
    > >
    > > def f(var=1):
    > > return var*2
    > >
    > > What value do I have to pass to f() if I want it to evaluate var to 1?
    > > I know that f() will return 2, but what if I absolutely want to pass a
    > > value to f()? "None" doesn't seem to work..

    >
    > If you *absolutely* want to pass a value and you don't know the default
    > value (otherwise you could just pass it):
    >
    > >>> import inspect
    > >>> v = inspect.getargspec(f)[3][0] # first default value
    > >>> f(v)

    > 2


    I have in fact a bunch of functions that all pass similar information
    to one main function. That function takes (amongst others) a template
    variable. If it's not being passed, it is set to a default value by the
    function called upon.

    For the moment, whenever a function calls the main function, I check
    whether the calling function has the template variable set:

    >>> if template:
    >>> return mainFunction(var, template)
    >>> else:
    >>> return mainFunction(var)


    Now, I thought this isn't the cleanest way to do things; so I was
    looking for ways to initialize the template variable, so that I could
    always return mainFunction(var, template). mainFunction() would then
    assign the default value to template.

    >From your answers, this seems to be impossible. The minute my variable

    is initialised, there's no way I can have mainFunction() assign a value
    without explicitly asking it to do so.

    I guess the best way would then be to change mainFunction from:
    >>> def mainFunction(var, template='base'):

    to
    >>> def mainFunction(var, template):
    >>> if len(template)=0:
    >>> template = 'base'


    and have the calling functions call mainFunction (var, template) and
    initialise template to ''.

    I still have that nagging feeling nicer code could be written to solve
    this, but I won't try to lose any sleep over it.

    Thanks for all the replies.
    LaundroMat, Oct 4, 2006
    #11
  12. LaundroMat

    Paul Rubin Guest

    "LaundroMat" <> writes:
    > def f(var=1):
    > return var*2
    >
    > What value do I have to pass to f() if I want it to evaluate var to 1?
    > I know that f() will return 2, but what if I absolutely want to pass a
    > value to f()? "None" doesn't seem to work..


    I don't understand your question. You can call f(var=1) just fine.
    Paul Rubin, Oct 4, 2006
    #12
  13. On 2006-10-04, Paul Rubin <http> wrote:
    > "LaundroMat" <> writes:
    >> def f(var=1):
    >> return var*2
    >>
    >> What value do I have to pass to f() if I want it to evaluate var to 1?
    >> I know that f() will return 2, but what if I absolutely want to pass a
    >> value to f()? "None" doesn't seem to work..

    >
    > I don't understand your question. You can call f(var=1) just fine.


    The problem is like the following.

    def f(var=1):
    return var*2

    def g():
    arg = None
    try:
    arg = Try_Processing() / 3 + 1
    except Nothing_To_Process:
    pass
    if arg is None:
    return f()
    else:
    return f(arg)

    Now in this case you could start by assigning arg the value 1 and
    eliminate the if test. However that only works if you know the
    default value for the argument. What he seems to be asking for
    is if there is an object, (let as call it Default), that would
    make code like:

    def f(var=1):

    Equivallent to:

    def f(var=Default)
    if var is Default)
    var = 1

    So that we could write the following without the need of the
    f's default value.

    def g():
    arg = Default
    try:
    arg = Try_Processing() / 3 + 1
    except Nothing_To_Process:
    pass
    f(arg)

    --
    Antoon Pardon
    Antoon Pardon, Oct 4, 2006
    #13
  14. LaundroMat

    LaundroMat Guest

    Antoon Pardon wrote:

    > The problem is like the following.
    >
    > def f(var=1):
    > return var*2
    >
    > def g():
    > arg = None
    > try:
    > arg = Try_Processing() / 3 + 1
    > except Nothing_To_Process:
    > pass
    > if arg is None:
    > return f()
    > else:
    > return f(arg)
    >
    > Now in this case you could start by assigning arg the value 1 and
    > eliminate the if test. However that only works if you know the
    > default value for the argument. What he seems to be asking for
    > is if there is an object, (let as call it Default), that would
    > make code like:
    >
    > def f(var=1):
    >
    > Equivallent to:
    >
    > def f(var=Default)
    > if var is Default)
    > var = 1
    >
    > So that we could write the following without the need of the
    > f's default value.
    >
    > def g():
    > arg = Default
    > try:
    > arg = Try_Processing() / 3 + 1
    > except Nothing_To_Process:
    > pass
    > f(arg)
    >
    > --
    > Antoon Pardon


    Exactly. Thanks for helping out.
    LaundroMat, Oct 4, 2006
    #14
  15. LaundroMat

    Paul Rubin Guest

    Antoon Pardon <> writes:
    > Now in this case you could start by assigning arg the value 1 and
    > eliminate the if test. However that only works if you know the
    > default value for the argument. What he seems to be asking for
    > is if there is an object, (let as call it Default), that would
    > make code like:
    >
    > def f(var=1):
    >
    > Equivallent to:
    >
    > def f(var=Default)
    > if var is Default)
    > var = 1


    Oh, I see. Yes, the OP should just use a distinct default value
    instead of 1. I usually do this with

    sentinel = object()

    def f(var=sentinel):
    if var is sentinel:
    # f was called without an arg
    Paul Rubin, Oct 4, 2006
    #15
  16. LaundroMat <> wrote:
    > I have in fact a bunch of functions that all pass similar information
    > to one main function. That function takes (amongst others) a template
    > variable. If it's not being passed, it is set to a default value by the
    > function called upon.
    >
    > For the moment, whenever a function calls the main function, I check
    > whether the calling function has the template variable set:
    >
    > >>> if template:
    > >>> return mainFunction(var, template)
    > >>> else:
    > >>> return mainFunction(var)

    >
    > Now, I thought this isn't the cleanest way to do things; so I was
    > looking for ways to initialize the template variable, so that I could
    > always return mainFunction(var, template). mainFunction() would then
    > assign the default value to template.
    >
    > From your answers, this seems to be impossible. The minute my variable
    > is initialised, there's no way I can have mainFunction() assign a value
    > without explicitly asking it to do so.
    >
    > I guess the best way would then be to change mainFunction from:
    > >>> def mainFunction(var, template='base'):

    > to
    > >>> def mainFunction(var, template):
    > >>> if len(template)=0:
    > >>> template = 'base'

    >
    > and have the calling functions call mainFunction (var, template) and
    > initialise template to ''.


    None is the traditional value to use for value not present, then you'd
    get this for the function

    def mainFunction(var, template=None):
    if template is None:
    template = 'base'

    And this for the calling bit

    if not_set_properly(template):
    template = None
    return mainFunction(var, template)

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
    Nick Craig-Wood, Oct 4, 2006
    #16
  17. Antoon Pardon <> wrote:
    > One possible way to do what I think you want is to code as follows:
    >
    > class Default (object):
    > pass


    I'd have written

    Default = object()

    > def f(var=Default):
    > if var is Default:
    > var = 1
    > return var * 2
    >


    But yes, defining a sentinel like this is a good idea.

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
    Nick Craig-Wood, Oct 4, 2006
    #17
  18. On 2006-10-04, Paul Rubin <http> wrote:
    > Antoon Pardon <> writes:
    >> Now in this case you could start by assigning arg the value 1 and
    >> eliminate the if test. However that only works if you know the
    >> default value for the argument. What he seems to be asking for
    >> is if there is an object, (let as call it Default), that would
    >> make code like:
    >>
    >> def f(var=1):
    >>
    >> Equivallent to:
    >>
    >> def f(var=Default)
    >> if var is Default)
    >> var = 1

    >
    > Oh, I see. Yes, the OP should just use a distinct default value
    > instead of 1. I usually do this with
    >
    > sentinel = object()
    >
    > def f(var=sentinel):
    > if var is sentinel:
    > # f was called without an arg


    But that can only work if you are the author of f. Take the
    following code:

    def myrepeat(obj, times = xxx):
    return itertools.repeat(obj, times)

    What value do I have to substitue for xxx, so that myrepeat
    will have the exact same function as itertools.repeat?

    --
    Antoon Pardon
    Antoon Pardon, Oct 4, 2006
    #18
  19. On 2006-10-04, Nick Craig-Wood <> wrote:
    > Antoon Pardon <> wrote:
    >> One possible way to do what I think you want is to code as follows:
    >>
    >> class Default (object):
    >> pass

    >
    > I'd have written
    >
    > Default = object()
    >
    >> def f(var=Default):
    >> if var is Default:
    >> var = 1
    >> return var * 2

    >
    > But yes, defining a sentinel like this is a good idea.


    The problem is that a lot of built in and library functions
    are not written this way. So when f is one of those, you
    are stuck.

    --
    Antoon Pardon
    Antoon Pardon, Oct 4, 2006
    #19
  20. LaundroMat

    Georg Brandl Guest

    Re: What value should be passed to make a function use the defaultargument value?

    Antoon Pardon wrote:
    > On 2006-10-04, Paul Rubin <http> wrote:
    >> Antoon Pardon <> writes:
    >>> Now in this case you could start by assigning arg the value 1 and
    >>> eliminate the if test. However that only works if you know the
    >>> default value for the argument. What he seems to be asking for
    >>> is if there is an object, (let as call it Default), that would
    >>> make code like:
    >>>
    >>> def f(var=1):
    >>>
    >>> Equivallent to:
    >>>
    >>> def f(var=Default)
    >>> if var is Default)
    >>> var = 1

    >>
    >> Oh, I see. Yes, the OP should just use a distinct default value
    >> instead of 1. I usually do this with
    >>
    >> sentinel = object()
    >>
    >> def f(var=sentinel):
    >> if var is sentinel:
    >> # f was called without an arg

    >
    > But that can only work if you are the author of f. Take the
    > following code:
    >
    > def myrepeat(obj, times = xxx):
    > return itertools.repeat(obj, times)
    >
    > What value do I have to substitue for xxx, so that myrepeat
    > will have the exact same function as itertools.repeat?


    There's no possible value. You'll have to write this like

    def myrepeat(obj, times=None):
    if times is None:
    return itertools.repeat(obj)
    else:
    return itertools.repeat(obj, times)

    Many functions implemented in C have this behavior.

    For all functions written in Python, you can look up the default
    value in the source.

    Georg
    Georg Brandl, Oct 4, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    493
    Joseph Byrns
    Apr 15, 2005
  2. jimjim
    Replies:
    3
    Views:
    389
    Buster
    Apr 16, 2004
  3. Anand
    Replies:
    2
    Views:
    887
    Anand
    Sep 11, 2003
  4. Replies:
    2
    Views:
    384
  5. Adam
    Replies:
    30
    Views:
    895
    Harald van Dijk
    Nov 11, 2008
Loading...

Share This Page