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

Antoon Pardon apardon at forel.vub.ac.be
Wed Oct 4 06:09:41 EDT 2006


On 2006-10-04, Paul Rubin <http> wrote:
> "LaundroMat" <Laundro at gmail.com> 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



More information about the Python-list mailing list