File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1060,6 +1060,20 @@ raise an exception when no appropriate method is defined.
10601060
10611061.. XXX above translation is not correct for new-style classes!
10621062
1063+ Special methods are only guaranteed to work if defined in an object's class, not
1064+ in the object's instance dictionary. That explains why this won't work::
1065+
1066+ >>> class C:
1067+ ... pass
1068+ ...
1069+ >>> c = C()
1070+ >>> c.__len__ = lambda: 5
1071+ >>> len(c)
1072+ Traceback (most recent call last):
1073+ File "<stdin>", line 1, in <module>
1074+ TypeError: object of type 'C' has no len()
1075+
1076+
10631077When implementing a class that emulates any built-in type, it is important that
10641078the emulation only be implemented to the degree that it makes sense for the
10651079object being modelled. For example, some sequences may work well with retrieval
You can’t perform that action at this time.
0 commit comments