[Python-ideas] Add kwargs to built-in function object

Erick Tryzelaar idadesub at users.sourceforge.net
Thu May 22 09:49:04 CEST 2008


On Thu, May 22, 2008 at 12:21 AM, Brandon Mintern <bmintern at gmail.com> wrote:
> I would like to propose to change the built-in function "object" to
> have the following syntax:
>
> object(**kwargs)
> Return a new featureless object. object is a base for all new style
> classes. It has the methods that are common to all instances of new
> style classes.
>
> If kwargs is given, the returned object's __dict__ will be kwargs (or
> something to that effect).

python 2.6 and 3.0 has something similar to this with collections.namedtuple:

http://docs.python.org/dev/3.0/library/collections.html#collections.namedtuple

The interface is a bit more verbose though:

>>> Point = namedtuple('Point', 'x y')
>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
33
>>> x, y = p                # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y               # fields also accessible by name
33
>>> p                       # readable __repr__ with a name=value style
Point(x=11, y=22)

I like the syntax of using arguments to object though. Maybe there's a
discussion behind namedtuple why they went that way instead of this
one?



More information about the Python-ideas mailing list