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

Bruce Leban bruce at leapyear.org
Thu May 22 17:36:26 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.
>
>
I agree this is a useful pattern. One thing I don't like about it is that:
   object(x=1, y=2)
looks very much like
   object(x=1, y=2, z=3)
and I probably won't discover I've been given the wrong one until it's too
late.

I think it can better addressed by implementing NamedList and NamedDict:

NamedList(typename, fieldnames[, optionalfields[, verbose]])

Returns a new list subclass named typename. The new subclass is used to
create list-like objects that have fields accessible by attribute lookup as
well as supporting other list operations. Instances of a NamedList may be
created using a mixture of positional and keyword arguments. If
optionalfields is not true, then the NamedList must always contain at least
as many elements as the number of fields. If the NamedDict contains fewer
elements than the number of fields, missing fields return None when accessed
by attribute (a.third) and raise IndexError when accessed by index (a[3]).

NamedDict(typename, fieldnames[, optionalfields[, verbose]])

Returns a new dict subclass named typename. The new subclass is used to
create dict-like objects that have fields accessible by attribute lookup as
well as supporting other dict operations. Instances of a NamedDict may be
created using keyword arguments only. If optionalfields is not true, then
the NamedDict must have a value for every field. If a NamedDict does not
contain a field, accessing it returns None when accessed by attribute (a.x)
and raises KeyError when accessed using by key (a['x']).

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080522/885f9cba/attachment.html>


More information about the Python-ideas mailing list