473,804 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Keyword arguments - strange behaviour?

Can anyone explain the behaviour of python when running this script?
def method(n, bits=[]): .... bits.append(n)
.... print bits
.... method(1) [1] method(2) [1, 2]

It's the same in python 1.5, 2.3 and 2.4 so it's not a bug. But I
expected the variable "bits" to be re-initialised to an empty list as
each method was called. Whenever I explain optional keyword arguments
to someone I have usually (wrongly as it turns out) said it is
equivalent to:
def method(n, bits=None): .... if bits is None:
.... bits=[]
.... bits.append(n)
.... print bits
.... method(1) [1] method(2)

[2]

Is there a good reason why these scripts are not the same? I can
understand how/why they are different, it's just not what I expected.
(It seems strange to me that the result of the first method can only be
determined if you know how many times it has already been called)

Is this behaviour what you would (should?) intuitively expect?
Thanks,

Brian

Jul 18 '05
24 2041
"harold fellermann" wrote:
I cannot see any strange behavior. this code works exacly as you and I suspect:


you seem to have missed some of the posts that led up to the one you
replied to. (most importantly, the first one).

</F>

Jul 18 '05 #11
br********@secu retrading.com wrote:
However, is there a good reason why default parameters aren't evaluated
as the function is called? (apart from efficiency and backwards
compatibility)?


So, one of my really common use cases that takes advantage of the fact
that default parameters are evaluated at function definition time:

def foo(bar, baz, matcher=re.comp ile(r'...')):
...
text = matcher.sub(r'. ..', text)
...

If default parameters were evaluated when the function was called, my
regular expression would get re-compiled every time foo() was called.
This would be inefficient, especially if foo() got called a lot. If
Python 3000 changed the evaluation time of default parameters, I could
rewrite this code as:

class foo(object):
matcher=re.comp ile(r'...')
def __new__(self, bar, baz, matcher=None):
if matcher is None:
matcher = self.matcher
...
text = matcher.sub(r'. ..', text)
...

But that seems like a lot of work to do something that used to be pretty
simple...

Steve
Jul 18 '05 #12
> Channelling the effbot, I think he was asking what namespace context
you
expected the expression "arg=otherfunct ion(x)" to be evaluated in when it's used at the time of a function call to dynamically create a new
default value for arg.


Thanks, I realise now that's what was meant. I think I was just
assuming otherfunction() would be globally available - but that's not
something I want to go into since I can see now there will be problems
trying to define what python should do if it evaluated defaults at the
function call :)

I think I can now explain to someone why there are good reasons that
the default params are evaluated at the definition. However, I still
can't give a nice looking solution on how to re-write a function to
have empty mutable values as default arguments: eg.

def method(a,b,opt1 =[],opt2=None,opt3 ="",opt4={})

How could I re-write this (especially if there are perhaps 20 optional
parameters,any number of which may have mutable defaults) without
writing 20 "if opt1 is None: opt1=[]" statements?

Brian

Jul 18 '05 #13

brian.b...@secu retrading.com wrote:
Channelling the effbot, I think he was asking what namespace context
you
expected the expression "arg=otherfunct ion(x)" to be evaluated in when
it's used at the time of a function call to dynamically create a

new default value for arg.


Thanks, I realise now that's what was meant. I think I was just
assuming otherfunction() would be globally available - but that's not
something I want to go into since I can see now there will be

problems trying to define what python should do if it evaluated defaults at the function call :)

I think I can now explain to someone why there are good reasons that
the default params are evaluated at the definition. However, I still
can't give a nice looking solution on how to re-write a function to
have empty mutable values as default arguments: eg.

def method(a,b,opt1 =[],opt2=None,opt3 ="",opt4={})

How could I re-write this (especially if there are perhaps 20 optional parameters,any number of which may have mutable defaults) without
writing 20 "if opt1 is None: opt1=[]" statements?

Brian


One way that is *slightly neater* is to simply collect all keyword
arguments as a dictionary. Then compare with a dictionary of defaults
for any missing keywords.

def afunction(**key wargs):
defaults = {'param1' : [], 'param2' : {}, 'param3' : []}
for entry in defaults:
if not keywargs.has_ke y(entry):
keywargs[entry] = defaults[entry]

This keeps all your defaults in a single dictionary and avoids a really
long function definition.
Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #14

brian.b...@secu retrading.com wrote:
Channelling the effbot, I think he was asking what namespace context
you
expected the expression "arg=otherfunct ion(x)" to be evaluated in when
it's used at the time of a function call to dynamically create a

new default value for arg.


Thanks, I realise now that's what was meant. I think I was just
assuming otherfunction() would be globally available - but that's not
something I want to go into since I can see now there will be

problems trying to define what python should do if it evaluated defaults at the function call :)

I think I can now explain to someone why there are good reasons that
the default params are evaluated at the definition. However, I still
can't give a nice looking solution on how to re-write a function to
have empty mutable values as default arguments: eg.

def method(a,b,opt1 =[],opt2=None,opt3 ="",opt4={})

How could I re-write this (especially if there are perhaps 20 optional parameters,any number of which may have mutable defaults) without
writing 20 "if opt1 is None: opt1=[]" statements?

Brian


One way that is *slightly neater* is to simply collect all keyword
arguments as a dictionary. Then compare with a dictionary of defaults
for any missing keywords.

def afunction(**key wargs):
defaults = {'param1' : [], 'param2' : {}, 'param3' : []}
for entry in defaults:
if not keywargs.has_ke y(entry):
keywargs[entry] = defaults[entry]

This keeps all your defaults in a single dictionary and avoids a really
long function definition.
Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #15

Steven Bethard wrote:
br********@secu retrading.com wrote:
However, is there a good reason why default parameters aren't evaluated as the function is called? (apart from efficiency and backwards
compatibility)?
So, one of my really common use cases that takes advantage of the

fact that default parameters are evaluated at function definition time:

def foo(bar, baz, matcher=re.comp ile(r'...')):
...
text = matcher.sub(r'. ..', text)
...

Surely "re.compile(r'. ..')" is effectively a constant ? So your above
code is equivalent to :

aConst = re.compile(r'.. .')
def foo(bar, baz, matcher=aConst) :
....
text = matcher.sub(r'. ..', text)
....

I agree that dynamic evaluation of default arguments seems much more
pythonic, as well as getting rid of a common python gotcha.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shmtl
If default parameters were evaluated when the function was called, my regular expression would get re-compiled every time foo() was called. This would be inefficient, especially if foo() got called a lot. If
Python 3000 changed the evaluation time of default parameters, I could rewrite this code as:

class foo(object):
matcher=re.comp ile(r'...')
def __new__(self, bar, baz, matcher=None):
if matcher is None:
matcher = self.matcher
...
text = matcher.sub(r'. ..', text)
...

But that seems like a lot of work to do something that used to be pretty simple...

Steve


Jul 18 '05 #16
Fuzzyman wrote:
Steven Bethard wrote:
br********@se curetrading.com wrote:
However, is there a good reason why default parameters aren't
evaluated as the function is called? (apart from efficiency
and backwards compatibility)?


So, one of my really common use cases that takes advantage of the
fact that default parameters are evaluated at function definition
time:

def foo(bar, baz, matcher=re.comp ile(r'...')):
...
text = matcher.sub(r'. ..', text)
...


Surely "re.compile(r'. ..')" is effectively a constant ? So your above
code is equivalent to :

aConst = re.compile(r'.. .')
def foo(bar, baz, matcher=aConst) :
...
text = matcher.sub(r'. ..', text)
...


Basically, yes. Though you add an extra name to the module or class
namespace by defining 'aConst'. I consider this a minor pollution of
the namespace since 'matcher' is only used in the function, not the
module or class.

But if we're arguing for equivalencies, the oft-asked for

def foo(lst=[]):
...

where [] is evaluated for each function call, is equivalent for most
purposes to:

def foo(lst=None):
if lst is None:
lst = []

There's a minor pollution of the range of values 'lst' can take on -- if
you need lst to be able to take on the value None, you'll want to
rewrite this something like:

no_value = object()
def foo(lst=no_valu e):
if lst is no_value:
lst = []

My point here is that there's always going to be some equivalent
expression (turing completeness etc.) I was just pointing out a quite
reasonable use of the current default parameter evaluation system.

Steve
Jul 18 '05 #17

Steven Bethard wrote:
Fuzzyman wrote:
Steven Bethard wrote:
br********@se curetrading.com wrote:

However, is there a good reason why default parameters aren't
evaluated as the function is called? (apart from efficiency
and backwards compatibility)?

So, one of my really common use cases that takes advantage of the
fact that default parameters are evaluated at function definition
time:

def foo(bar, baz, matcher=re.comp ile(r'...')):
...
text = matcher.sub(r'. ..', text)
...
Surely "re.compile(r'. ..')" is effectively a constant ? So your above code is equivalent to :

aConst = re.compile(r'.. .')
def foo(bar, baz, matcher=aConst) :
...
text = matcher.sub(r'. ..', text)
...


Basically, yes. Though you add an extra name to the module or class
namespace by defining 'aConst'. I consider this a minor pollution of

the namespace since 'matcher' is only used in the function, not the
module or class.

But if we're arguing for equivalencies, the oft-asked for

def foo(lst=[]):
...

where [] is evaluated for each function call, is equivalent for most
purposes to:

def foo(lst=None):
if lst is None:
lst = []

There's a minor pollution of the range of values 'lst' can take on -- if you need lst to be able to take on the value None, you'll want to
rewrite this something like:

no_value = object()
def foo(lst=no_valu e):
if lst is no_value:
lst = []

My point here is that there's always going to be some equivalent
expression (turing completeness etc.) I was just pointing out a quite reasonable use of the current default parameter evaluation system.

Steve


Sure.. but you also gave an example of an alternative that was complex,
and used it's complexity as an argument against having default
arguments dynamically evaluated. What I was saying is that there is an
alternative that is at least as readable (if not more) than your
example.

Having default arguments evaluated when the function is defined is
unintuitive and unpythonic.
Regards,
Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #18
Fuzzyman wrote:

[...]

Having default arguments evaluated when the function is defined is
unintuitive and unpythonic.


With due respect that's a matter of opinion.

Having default arguments evaluated when the function is defined is the
result of several design decisions in Python, not the least of which is
the decision to have "def" be an executable statement. Ever wondered how
come you can def the same function name several times in the same
module? It's simply because on of def's side-effects is the binding of
its name in the current namespace.

Unfortunately there's absolutely no guarantee that the namespace context
when a function is called will bear any relation to the namespace
context when it was defined (particularly given that definition and call
can take place in entirely separate modules). So, how would one specify
a "deferred expression" to be evaluated when the function is called
rather than when it is defined? There presumably has to be some
technique that's different from the current ones, since presumably you
would also want to retain the option of having the default evaluated at
definition time.

I still haven't seen any reasonable suggestions as to how one might
exactly specify the execution context for such a deferred expression,
let alone how to spell the deferred expression itself. Would you have us
provide a code object that can be eval()'d?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #19
Fuzzyman wrote:
Steven Bethard wrote:

So, one of my really common use cases that takes advantage of the
fact that default parameters are evaluated at function definition
time:

def foo(bar, baz, matcher=re.comp ile(r'...')):
...
text = matcher.sub(r'. ..', text)
...

Sure.. but you also gave an example of an alternative that was complex,


Interesting. I would have thought that my example was pretty simple.
Maybe it would be helpful to generalize it to:

def foo(bar, baz, spam=badger(x, y, z)):
...

All it does is use a default value that was produced by a function call.
I'm surprised you haven't run into this situation before...

Of course, what is complex or simple is a matter of personal opinion. I
use this pattern so often that it's quite simple to me, but I guess I
can understand that if you don't use such a pattern, it might seem
foreign to you.

Steve
Jul 18 '05 #20

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

Similar topics

14
5280
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while for keyword arguments the tutorial shows: def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
24
3308
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
14
2209
by: spibou | last post by:
What is strcmp supposed to return if one or both arguments passed to it are NULL ?
8
1686
by: matthewperpick | last post by:
Check out this toy example that demonstrates some "strange" behaviour with keyword arguments and inheritance. ================================= class Parent: def __init__(self, ary = ): self.ary = ary def append(self):
17
3383
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a segmentation fault on the next line, followed by other strange
10
5134
by: Armando Serrano Lombillo | last post by:
Why does Python give an error when I try to do this: Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> len(object=) TypeError: len() takes no keyword arguments but not when I use a "normal" function: return len(object)
20
1284
by: Jasper | last post by:
I'm stumped. I'm calling a method that has keyword args, but not setting them, and yet one of them starts off with data?! The class definition begins like so: class BattleIntentionAction( BattleAction ): def __init__( self, factionName, location, tactic='hold', targetFacName='', terrainArgs=, garrisonIds= ): self.terrainArgs = terrainArgs print terrainArgs
0
9707
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...
1
10323
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
9161
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
7622
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
5525
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.