I am reading DiveIntoPython.
I'm wondering why I can directly print the instance of a UserDict as a dictionary. In detail, these codes
import UserDict;
d = UserDict.UserDict({1:1, 'a':2});
print d;
print d.data;
will have output
{'a': 2, 1: 1}
{'a': 2, 1: 1}
And these codes
class MyDict:
def __init__(self, dictData=None):
self.data = dictData;
d = MyDict({1:1, 'a':2});
print d;
print d.data;
will have output (on my machine)
<__main__.MyDict instance at 0x10049ef80>
{'a': 2, 1: 1}
In other words, How I can define my class, and print its instances as a built-in datatype? Thank you!