Using dictionaries

Alex Martelli aleaxit at yahoo.com
Wed Dec 6 04:26:06 EST 2000


"sorular" <sorular at netscape.net> wrote in message
news:3a2dca59.583722939 at news-server.bigpond.net.au...
    [snip]
> The dictionary would be {'UserXXXX':'123123'}
>
> ----Configuration.py (start)----
> ...
> UserName=UserXXXX
> UserNumb="123123"
> ...

I'll assume you mean

UserName='UserXXXX'

as these quotes are really crucial.


> self.authenticate=Dict_lookup(UserName=function(UserNumb))

This passes to this __init__:

> class Dict_lookup:
>     def __init__(self,**kw):
>         self.dict=kw

a 'kw' dictionary like {'UserName': function(UserNumb)}.

In other words, in the sintax:

    callable(aname=avalue)

aname is NOT looked up anywhere: {'aname': avalue} is the
resulting keywords-dictionary (which the callable can
retrieve as **kw).  'aname' is taken as *constant* in
this syntactic form.


Try, instead (Python 2 syntax):

self.authenticate=Dict_lookup(**{UserName:function(UserNumb)})

here, you are explicitly building and passing the keywords
dictionary, rather than relying on it being built for you
by the Python compiler and interpreter; so, you can use any
expressions you desire for the keys as well as for the values,
rather than being limited to constant keys as in the name=value
keywords-syntax.

Alternatively, save yourself the double stars on both the
call and the function definition:

class Dict_lookup:
    def __init__(self,kw):
        self.dict=kw

self.authenticate=Dict_lookup({UserName:function(UserNumb)})

and the net effect will be the same (though it will also
work on older versions of Python; the double-stars syntax
on the call only works in Python 2).


Alex






More information about the Python-list mailing list