Ian Kelly <[email protected]>:
> On Wed, Apr 6, 2016 at 1:59 PM, Marko Rauhamaa <[email protected]> wrote:
>> It seems to me CPython is being a bit too picky here. Why should it
>> care if the method is a class method or an object method?
>
> Because the purpose of a class is to define the behavior of its
> instances. A function stored in an object attribute technically isn't
> a method at all. It's just a function that happens to be stored in an
> object attribute, i.e. *data*. Why should the behavior of a
> SimpleNamespace change just because somebody decided they wanted to
> store something under the name "__iter__" (or worse,
> "__getattribute__")?
>
> Also, because not having to check the instance dict for the presence
> of dunder methods is faster.
Not convinced. Probably just an oversight.
For example, file-like objects have no such reservations:
import sys
import types
import xml.dom.minidom
filelike = types.SimpleNamespace()
def write(s):
for c in s:
sys.stdout.write("{{{}}}".format(c))
filelike.write = write
filelike.close = lambda: None
doc = xml.dom.minidom.getDOMImplementation().createDocument(
None, "tag", None).writexml(filelike)
sys.stdout.write("\n")
Marko
--
https://mail.python.org/mailman/listinfo/python-list