1st Sketch at at ReadOnly dict

Chris Angelico rosuav at gmail.com
Mon Jan 20 19:08:32 EST 2014


On Tue, Jan 21, 2014 at 7:09 AM, Charles Hixson
<charleshixsn at earthlink.net> wrote:
> #    @note Instances can be created only from existing dicts.
>
>     ##    Class initializer.
>     #    @param    ddict    The data dictionary to which this is a read only
>     #            access.
>     #    @throws    TypeError if ddict is not a dict.
>     def __init__ (self, ddict = {}):
>         if not isinstance(ddict, dict):
>             raise    TypeError("ddict must be a dict.  It is " +
> repr(ddict))
>         self._ddict    =    ddict

Instead of demanding that a dict (or dict subclass) be passed, why not
simply pass all args on to dict() itself? Is there a reason this won't
work?

def __init__(self, *args, **kwargs):
    self._ddict = dict(*args, **kwargs)

ChrisA



More information about the Python-list mailing list