En Tue, 13 Feb 2007 23:54:29 -0300, <[EMAIL PROTECTED]> escribió: >>>> from UserDict import UserDict >>>> d = {1:2,3:4,5:6} >>>> d1 = UserDict(d) >>>> d1 > {1: 2, 3: 4, 5: 6} >>>> d1.data > {1: 2, 3: 4, 5: 6} > Here why both d1 and d1.data have the same values?As shown below,they're > different types. > >>>> type(d1) > <type 'instance'> >>>> type(d1.data) > <type 'dict'> > Please help.Thanks!
UserDict is a specially crafted class that looks and acts like a dictionary, so, its str() and repr() looks like a true dictionary. But as you have seen, using type() you can distinguish between the two. d1.__class__ is different too, as many other details; but many functions don't require a "true" dictionary anyway, just use methods like keys(), items(), and magic support for d[key], d[key]=value, and so on. As far as UserDict provides those methods, all functions expecting a dictionary-like argument are happy. An historical note: On older Python versions, you could not inherit from builtin types. If you wanted a custom dictionary-like class, you had to inherit from UserDict or a similar class. Now, you can inherit from dict if you want; and DictMixin can be used to provide any class with dictionary functionalities in a much convenient way. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list