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

Thanks in advance.

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'

I'm not sure I follow your problem...what's stopping you from
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..
>
> Thanks in advance.
>

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

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

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

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

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post AikidoGuy C Programming 11 11-21-2011 10:43 PM Reckoner Python 11 01-19-2009 03:31 AM kuangye C++ 2 04-10-2008 07:58 AM komal C++ 6 01-25-2005 11:13 AM Tito Python 3 07-20-2004 10:24 PM

Advertisments