473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Thanks in advance.

Oct 3 '06
50 3333
Georg Brandl wrote:
>But that can only work if you are the author of f. Take the
following code:

def myrepeat(obj, times = xxx):
return itertools.repea t(obj, times)

What value do I have to substitue for xxx, so that myrepeat
will have the exact same function as itertools.repea t?

There's no possible value. You'll have to write this like

def myrepeat(obj, times=None):
if times is None:
return itertools.repea t(obj)
else:
return itertools.repea t(obj, times)
or:

def myrepeat(*args) :
return itertools.repea t(*args)

</F>

Oct 4 '06 #21
On 2006-10-04, Georg Brandl <g.************ *@gmx.netwrote:
Antoon Pardon wrote:
>On 2006-10-04, Paul Rubin <httpwrote:
>>Antoon Pardon <ap*****@forel. vub.ac.bewrites :
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):

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

But that can only work if you are the author of f. Take the
following code:

def myrepeat(obj, times = xxx):
return itertools.repea t(obj, times)

What value do I have to substitue for xxx, so that myrepeat
will have the exact same function as itertools.repea t?

There's no possible value. You'll have to write this like
Yes, that was the point I wanted to make.
def myrepeat(obj, times=None):
if times is None:
return itertools.repea t(obj)
else:
return itertools.repea t(obj, times)

Many functions implemented in C have this behavior.
Which is a pity and IMO makes the documentation of these
functions a bit problematic. Take the itertool.repeat
documentation:

repeat(object[, times])
Make an iterator that returns object over and over again. Runs
indefinitely unless the times argument is specified. ...

My first impression from this, is that it is possible to call
this as follows:

repeat(None, times = 5)

But that doesn't work either.
For all functions written in Python, you can look up the default
value in the source.
That wont help much if you would like something like the following:

def fun(f):

arg = Default
try:
arg = Try_Processing( )
except Nothing_To_Proc ess:
pass
f(arg)

--
Antoon Pardon
Oct 4 '06 #22
On 2006-10-04, Fredrik Lundh <fr*****@python ware.comwrote:
Georg Brandl wrote:
>>But that can only work if you are the author of f. Take the
following code:

def myrepeat(obj, times = xxx):
return itertools.repea t(obj, times)

What value do I have to substitue for xxx, so that myrepeat
will have the exact same function as itertools.repea t?

There's no possible value. You'll have to write this like

def myrepeat(obj, times=None):
if times is None:
return itertools.repea t(obj)
else:
return itertools.repea t(obj, times)

or:

def myrepeat(*args) :
return itertools.repea t(*args)
Yes that works but I have the impression that this solution
becomes complicated very fast once you want to do extra
processing in the function body. Take the following

def myrepeat(obj, times = xxx)"
newobj = Process(obj)
return itertools.repea t(obj, times)

I think it would become something like:

def myrepeat(*args) :
obj = args[0]
tail = args[1:]
newobj = Process(obj)
newargs = (newobj,) + tail
return itertools.repea t(*newargs)
Oct 4 '06 #23
Antoon Pardon <ap*****@forel. vub.ac.bewrites :
repeat(object[, times])
Make an iterator that returns object over and over again. Runs
indefinitely unless the times argument is specified. ...

My first impression from this, is that it is possible to call
this as follows:
repeat(None, times = 5)
But that doesn't work either.
The code and/or doc is wrong, you have to use a positional arg
and not a named one. repeat(None, 5) does the right thing.
That wont help much if you would like something like the following:

def fun(f):

arg = Default
try:
arg = Try_Processing( )
except Nothing_To_Proc ess:
pass
f(arg)
Write it like this:

def fun(f):
args = ()
try:
args = (Try_Processing (),)
except Nothing_To_Proc ess:
pass
f(*args)
Oct 4 '06 #24
Antoon Pardon <ap*****@forel. vub.ac.bewrites :
I think it would become something like:

def myrepeat(*args) :
obj = args[0]
tail = args[1:]
newobj = Process(obj)
newargs = (newobj,) + tail
return itertools.repea t(*newargs)
Too messy. Just write:

def myrepeat(obj, *times):
return itertools.repea t(Process(obj), *times)
Oct 4 '06 #25
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..

Thanks in advance.
I think the only general solution for your problem would be to
define a defaulter function. Something like the following:

Default = object()

def defaulter(f, *args):

while args:
if args[-1] is Default:
args = args[:-1]
else:
break
return f(*args)
The call:

defaulter(f, arg1, arg2, Default, ..., Default)

would then be equivallent to:

f(arg1, arg2)

Or in your case you would call:

defaulter(f, Default)

--
Antoon Pardon
Oct 4 '06 #26
On 2006-10-04, Antoon Pardon <ap*****@forel. vub.ac.bewrote:
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..

Thanks in advance.

I think the only general solution for your problem would be to
define a defaulter function. Something like the following:

Default = object()

def defaulter(f, *args):

while args:
if args[-1] is Default:
args = args[:-1]
else:
break
return f(*args)
The call:

defaulter(f, arg1, arg2, Default, ..., Default)

would then be equivallent to:

f(arg1, arg2)

Or in your case you would call:

defaulter(f, Default)
A little update, with the functools in python 2.5 you
could turn the above into a decorator. Something like
the following (not tested):

def defaulting(f):
return functools.parti al(defaulter, f)

You could then simply write:

@defaulting
def f(var=1):
return var * 2

And for built in or library functions something like:

from itertools import repeat
repeat = defaulting(repe at)

--
Antoon Pardon
Oct 4 '06 #27
Paul Rubin wrote:
Antoon Pardon <ap*****@forel. vub.ac.bewrites :
>repeat(objec t[, times])
Make an iterator that returns object over and over again. Runs
indefinitely unless the times argument is specified. ...

My first impression from this, is that it is possible to call
this as follows:
repeat(None, times = 5)
But that doesn't work either.

The code and/or doc is wrong, you have to use a positional arg
and not a named one. repeat(None, 5) does the right thing.
This is an issue in most Python documentation: you're not told
if the described function is implemented in C, and if it is
keyword arg-enabled. The arguments must be given names though,
to be able to document them.

Georg
Oct 4 '06 #28
Georg Brandl wrote:
This is an issue in most Python documentation: you're not told
if the described function is implemented in C, and if it is
keyword arg-enabled. The arguments must be given names though,
to be able to document them.
the general rule is that if the documentation doesn't explicitly say
that something is a keyword argument, it isn't, and shouldn't be treated
as such.

</F>

Oct 4 '06 #29
On 2006-10-04, Fredrik Lundh <fr*****@python ware.comwrote:
Georg Brandl wrote:
>This is an issue in most Python documentation: you're not told
if the described function is implemented in C, and if it is
keyword arg-enabled. The arguments must be given names though,
to be able to document them.

the general rule is that if the documentation doesn't explicitly say
that something is a keyword argument, it isn't, and shouldn't be treated
as such.
The first module I looked in to check this, it wasn't true. In the Queue
Module is isn't explicitly written that maxsize is a keyword argument yet
Queue.Queue(max size=9) works just fine.

I then took a look in the Threading module and found that the semaphore
documentation didn't mention anything about keyword arguments but
again they worked fine.

It wouldn't surprise me if this was true for the complete threading
documentation.

--
Antoon Pardon
Oct 5 '06 #30

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

Similar topics

3
8903
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
3
2838
by: Tito | last post by:
From the Python's tutorial, about default argument values: <quote> The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls: def f(a, L=): L.append(a) return L
20
2569
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as following, delimited by //////: ////////////////pose.h #ifndef pose_h #define pose_h #include "point.h"
140
7915
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
21
2252
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was wondering if someone had a program like this..it has to send e-mails, read e-mails from a .txt file and show them in the screen with the subject, date, etc... can someone please help me out? thanks
23
3650
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a so-called pre-fix notation or algebraic notation. Algebraic notations have the concept of operators, meaning, symbols placed around arguments. In algebraic in-fix notation, different
12
2595
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
4
6697
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
16
3450
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName), _netguid(netguid) {}
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.