function parameter: question about scope

Emile van Sebille emile at fenx.com
Sun Jul 14 12:14:08 EDT 2002


Uwe Mayer
> def getElements(self, types, memo={}):
[snip]
> The problem is, that 'memo' seems to live beyond method invocation -
and
> I don't know why:

When you use:

def getElements(self, types, memo={}):

memo will only be created once when the module is initially imported.
All subsequent access to getElements() that don't specifically override
memo share the same originally created copy of memo.  You probably want
to do:

def getElements(self, types, memo=None):
    if memo == None:
        memo = {}

Then each invocation of getElements not passing in memo will get a newly
created memo.

HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list