function parameter: question about scope

Uwe Mayer merkosh at hadiko.de
Sun Jul 14 21:06:57 EDT 2002


hi,

I've got a object method which works similar to the __deepcopy__() 
function of the copy/deepcopy protocol. this function takes an optional
argument "memo={}":

def getElements(self, types, memo={}):
    '''Returns a list of sub-elements.'''
    if memo.has_key(id(self)): return memo[id(self)]
    elements = super(Compound, self).getElements(types, memo)
    if (types == ()) or (self.__class__ in types): elements.extend
([self])
    return elements


This function checks wether self.__class__ is contained in 'types' and 
if so, it adds itself to the list 'elements' and returns it.

The problem is, that 'memo' seems to live beyond method invocation - and 
I don't know why:
If I call 'getElements( types=() )', the method works fine. The first 
call to 'getElements( types=(<someClass>) )' works fine, too. The 
parameter 'memo' seems to have remembered <someClass>, because when I 
call 'getElements( types=() )' again I get the same result as in the 
previous call which is different to the first call.

Calling 'getElements( types=(), memo={} )' works fine.

Why does 'memo' keep exising?

Thanks in advance
Uwe



More information about the Python-list mailing list