[Python-Dev] dict() enhancement idea?

Oren Tirosh oren-py-d@hishome.net
Thu, 21 Nov 2002 09:12:51 -0500


On Thu, Nov 21, 2002 at 01:51:15PM +0100, Just van Rossum wrote:
> I sometimes use an idiom like
> 
>   def dictfromkeywords(**kwargs):
>       return kwargs
> 
>   d = dictfromkeywords(
>           akey = 12,
>           anotherkey = "foo",
>           ...etc.
>   )

I assume that the motive is to get rid of the quotes around the key and
conceptually treat it as a "symbol" rather than as a string.  If that is 
the case it could apply to access as well as initialization.

class record(dict):
    def __init__(self, __initfrom=(), **kw):
        self.__dict__ = self
        dict.__init__(self, __initfrom)
        self.update(kw)

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__,
          ', '.join(['%s=%s' % (k, repr(v)) for k,v in self.items()]))

Fields can be accessed as either items or attributes of a record object.

	Oren