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

L

LaundroMat

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.
 
L

Lawrence Oluyede

LaundroMat said:
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?
 
F

Fredrik Lundh

LaundroMat said:
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>
 
T

Tim Chase

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..
.... return var*2
....'hellohello'


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

-tkc
 
L

Larry Bates

LaundroMat said:
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
 
T

Thomas Jollans

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).... return v*2
....
f() 2
f(1) 2
f(*[1]) 2
f(*[]) 2

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

Stan Graves

LaundroMat said:
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?
.... if var == None:
.... var = 1
.... return 2*var
....
--Stan Graves
 
B

Bruno Desthuilliers

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

Rob De Almeida

LaundroMat said:
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
 
A

Antoon Pardon

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
 
L

LaundroMat

Rob said:
LaundroMat said:
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:

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:
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.
 
P

Paul Rubin

LaundroMat said:
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.
 
A

Antoon Pardon

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)
 
L

LaundroMat

Antoon said:
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)

Exactly. Thanks for helping out.
 
P

Paul Rubin

Antoon Pardon said:
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
 
N

Nick Craig-Wood

LaundroMat said:
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:


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:

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)
 
N

Nick Craig-Wood

Antoon Pardon said:
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.
 
A

Antoon Pardon

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

Antoon Pardon

I'd have written

Default = object()


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.
 
G

Georg Brandl

Antoon said:
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
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top