Re: Getting around immutable default arguments for recursion

2009-01-14 Thread alex23
dpapathanasiou wrote: > Passing the entire dictionary to every function that accesses it is > better? If there are a large number of functions, you could combine them and the history_db dict into a single object. -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread Steven D'Aprano
On Wed, 14 Jan 2009 15:27:01 -0800, dpapathanasiou wrote: >> a) a global should and need not be used. > > Passing the entire dictionary to every function that accesses it is > better? Yes. There is very little overhead when passing objects to functions in Python. There's no performance penalty

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread James Mills
On Thu, Jan 15, 2009 at 9:27 AM, dpapathanasiou wrote: > Without the "if priors:" line just above the first return statement (a > typo perhaps?), then yes, it would do what I want. Yes sorry it was :) >> a) a global should and need not be used. > > Passing the entire dictionary to every function

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread dpapathanasiou
> How about this then: > > def get_prior_versions (item_id, priors=None): >"""Return a list of all prior item ids starting with this one""" >global history_db # key = item id, value = prior item id >prior_id = history_db[item_id] >if not prior_id: >if priors: >retur

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread dpapathanasiou
> You'll continue to be confused if you use that term. Python already > has a specific use of the term “immutable”, and it doesn't apply > here. I was just following the terminology used in "A Byte of Python" (which, that particular point aside, is a very good tutorial). > Better to say: default

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread James Mills
On Thu, Jan 15, 2009 at 8:32 AM, dpapathanasiou wrote: (...) > It's not exactly right for what I'm doing, b/c the caller always > expects a list in return. How about this then: def get_prior_versions (item_id, priors=None): """Return a list of all prior item ids starting with this one"""

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread Ben Finney
dpapathanasiou writes: > But every subsequent call returned the results of the prior call, > plus the results of the current call. > > I was confused until I read in the docs that default arguments are > immutable. You'll continue to be confused if you use that term. Python already has a specif

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread dpapathanasiou
> How about: > > def get_prior_versions (item_id, priors=None): >"""Return a list of all prior item ids starting with this one""" >global history_db # key = item id, value = prior item id >prior_id = history_db[item_id] >if not prior_id: >return priors >else: >i

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread dpapathanasiou
> The usual solution is: > > def get_prior_versions (item_id, priors=None): > if priors is None: > priors = [] Thanks! -- http://mail.python.org/mailman/listinfo/python-list

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread James Mills
On Thu, Jan 15, 2009 at 8:11 AM, dpapathanasiou wrote: > I wrote this function to retrieve a list of items from a dictionary. > > The first time it was called, it worked properly. > > But every subsequent call returned the results of the prior call, plus > the results of the current call. > > I wa

Re: Getting around immutable default arguments for recursion

2009-01-14 Thread MRAB
dpapathanasiou wrote: I wrote this function to retrieve a list of items from a dictionary. The first time it was called, it worked properly. But every subsequent call returned the results of the prior call, plus the results of the current call. I was confused until I read in the docs that defa

Getting around immutable default arguments for recursion

2009-01-14 Thread dpapathanasiou
I wrote this function to retrieve a list of items from a dictionary. The first time it was called, it worked properly. But every subsequent call returned the results of the prior call, plus the results of the current call. I was confused until I read in the docs that default arguments are immuta