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

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

LaundroMat
Guest
Posts: n/a

 10-03-2006
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..

Lawrence Oluyede
Guest
Posts: n/a

 10-03-2006
LaundroMat <(E-Mail Removed)> 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

Fredrik Lundh
Guest
Posts: n/a

 10-03-2006
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>

Tim Chase
Guest
Posts: n/a

 10-03-2006
> 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

Larry Bates
Guest
Posts: n/a

 10-03-2006
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

Thomas Jollans
Guest
Posts: n/a

 10-03-2006
On Tue, 03 Oct 2006 13:16:57 -0700, "LaundroMat" <(E-Mail Removed)> 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..
>

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

Stan Graves
Guest
Posts: n/a

 10-03-2006
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

Bruno Desthuilliers
Guest
Posts: n/a

 10-03-2006
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) ?

Rob De Almeida
Guest
Posts: n/a

 10-03-2006
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

Antoon Pardon
Guest
Posts: n/a

 10-04-2006
On 2006-10-03, LaundroMat <(E-Mail Removed)> 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..
>

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