Search code examples
pythonobject-literal

Are object literals Pythonic?


JavaScript has object literals, e.g.

var p = {
  name: "John Smith",
  age:  23
}

and .NET has anonymous types, e.g.

var p = new { Name = "John Smith", Age = 23}; // C#

Something similar can be emulated in Python by (ab)using named arguments:

class literal(object):
    def __init__(self, **kwargs):
        for (k,v) in kwargs.iteritems():
            self.__setattr__(k, v)
    def __repr__(self):
        return 'literal(%s)' % ', '.join('%s = %r' % i for i in sorted(self.__dict__.iteritems()))
    def __str__(self):
        return repr(self)

Usage:

p = literal(name = "John Smith", age = 23)
print p       # prints: literal(age = 23, name = 'John Smith')
print p.name  # prints: John Smith

But is this kind of code considered to be Pythonic?


Solution

  • Have you considered using a named tuple?

    Using your dict notation

    >>> from collections import namedtuple
    >>> L = namedtuple('literal', 'name age')(**{'name': 'John Smith', 'age': 23})
    

    or keyword arguments

    >>> L = namedtuple('literal', 'name age')(name='John Smith', age=23)
    >>> L
    literal(name='John Smith', age=23)
    >>> L.name
    'John Smith'
    >>> L.age
    23
    

    It is possible to wrap this behaviour into a function easily enough

    def literal(**kw):
        return namedtuple('literal', kw)(**kw)
    

    the lambda equivalent would be

    literal = lambda **kw: namedtuple('literal', kw)(**kw)
    

    but personally I think it's silly giving names to "anonymous" functions