Extending the dict class

Tim N. van der Leeuw tnleeuw at gmail.com
Tue Aug 29 09:30:46 EDT 2006


Fredrik Lundh wrote:
> "chosechu" wrote:
>
> > Yes, if I could simply modify myfunc() I would have workarounds.
> > This would mean me modifying SOAPpy and specializing it for
> > my needs.
>
> maybe you could fake it:
>
No you cannot fake it, because the ** form of passing arguments
constructs a new dictionary (and that dictionary is the standard
built-in type of dictionary, not your own fake-dictionary).

Just did some tests, redefining the dict-type:

>>> native_dict = dict
>>> type({}) == native_dict
True
>>> class my_dict(native_dict):
... 	pass
...
>>> dict = my_dict
>>> type({}) == native_dict
True
>>> type(dict()) == native_dict
False
>>> def foo(**kwargs):
... 	print type(kwargs) == native_dict
...
>>> foo(x=1)
True
>>> foo(**dict())
True
>>>

(NB: I also tried changing __builtins__.dict, no effect)

As the OP said, when passing keyword-arguments, a new instance of a
built-in dict is always generated and not the dict type bound to the
dict-constructor.

I do actually think that it would be useful in many circumstances to be
able to replace the default-constructor of a particular so that custom
objects are created for that type.

OTOH, there are probably many more cases where doing so would be a very
bad idea, not a good idea, and we would begin seeing an overwhelming
number of cases of mis-use of such feature.

Cheers,

--Tim




More information about the Python-list mailing list