On Wed, Apr 6, 2016 at 1:59 PM, Marko Rauhamaa <ma...@pacujo.net> wrote: > Ian Kelly <ian.g.ke...@gmail.com>: > >> On Wed, Apr 6, 2016 at 1:22 PM, Marko Rauhamaa <ma...@pacujo.net> wrote: >>> Why is a SimpleNamespace object not an iterator even though it >>> provides __iter__ and __next__? >> >> Because Python expects those methods to be defined in the class dict, >> not the instance dict. > > The documentation does state: > > these are used to allow user-defined classes to support iteration > > although the rest of it only refers to objects. > > 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. -- https://mail.python.org/mailman/listinfo/python-list