473,856 Members | 1,834 Online

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

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

Oct 3 '06 #1
50 3355
LaundroMat <La*****@gmail. comwrote:
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
Oct 3 '06 #2
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>

Oct 3 '06 #3
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'
passing a value?

-tkc

Oct 3 '06 #4
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..

The answer is don't pass any value.

print f()

will print 2

-Larry Bates
Oct 3 '06 #5
On Tue, 03 Oct 2006 13:16:57 -0700, "LaundroMat " <La*****@gmail. comlet
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..

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
>>>
*[list] is the reverse of def f(*args)

--
Thomas Jollans alias free-zombie
Oct 3 '06 #6
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..

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

Oct 3 '06 #7
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) ?
Oct 3 '06 #8
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.getargs pec(f)[3][0] # first default value
f(v)
2

Oct 3 '06 #9
On 2006-10-03, LaundroMat <La*****@gmail. comwrote:
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..

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
Oct 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.