retrieving from list

Darrell Gallion darrell at dorb.com
Tue May 30 00:01:16 EDT 2000


Marc Tardif" <intmktg at Gloria.CAM.ORG>
>
> The last line is where the problem lies. How can 'arg' be able to refer to
> 'foo' or 'bar'? Should 'arg' be a string of comma seperated values which
> would be parsed within the function in order to refer to the proper member
> of the list? For example, '0' for 'foo' and '0,0' for 'bar'?

def func(*arg):
    a = []
    a.append('foo')
    b = []
    b.append('bar')
    a.append(b)
    # now I have a = ['foo', ['bar']]
    res=a
    for x in arg:
        try:
            assert(type(res)==type(a))
            res=res[x]
        except:
            res=None
            break
    return res


print `func(0)`
print `func(1)`
print `func(0,0)`
print `func(1,0)`

############# Output
'foo'
['bar']
None
'bar'







More information about the Python-list mailing list