> Notwithstanding the "perverted" implementation, Alex's idea is > absolutely wonderful and addresses a core usability issue with > classmethods. I'm not so sure. I think the main issue is that Python users aren't used to static methods; C++ and Java users should be familiar with them and I don't think they cause much trouble there. > If only in the C API, I would like to see just such a universalmethod > alternative to classmethod. That would allow different behaviors to be > assigned depending on how the method is called. > > Both list.sort() and dict.fromkeys() would benefit from it: > > > class MagicDict(dict): > > def _class_fromkeys(cls, lst, value=True): > "Make a new dict using keys from list and the given value" > obj = cls() > for elem in lst: > obj[elem] = value > return obj > > def _inst_fromkeys(self, lst, value=True): > "Update an existing dict using keys from list and the given value" > for elem in lst: > self[elem] = value > return self > > newfromkeys = MagicDescriptor(_class_fromkeys, _inst_fromkeys) > > print MagicDict.newfromkeys('abc') > print MagicDict(a=1, d=2).newfromkeys('abc') But your _inst_fromkeys mutates self! That completely defeats the purpose (especially since it also returns self) and I am as much against this (approx. -1000 :-) as I am against sort() returning self. To me this pretty much proves that this is a bad idea; such a schizo method will confuse users more that a class method that ignores the instance. And if you made an honest mistake, and meant to ignore the instance, it still proves that this is too confusing to do! :-) --Guido van Rossum (home page: http://www.python.org/~guido/)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4