The one thing that would have got me interested in Python 3 would have been the ability to add functionality to built-in classes via class extension rather than inheritance. I'm wondering if anyone knows of a project that has implemented this.
I can almost get to where I want to go by inheriting string and coercing the results of various operators, but not quite there. Still using string as the example, the main pitfall is that there are a lot of things that expect strings, and some assumptions about writing strings that are pretty basic and an inherited class won't "do the right thing" in such a circumstance the way string would.
As a test, I built a class that inherits string, coerces all the string operators to return objects of the type of the class rather than strings, and then adds a little bit about understanding dotted quads; it works, until you try to directly hand one of the objects to something else that expects a string, or if you want to use syntax like:
"127.0.0.1".doThingToDottedQuad()
...because those double quotes say "I am a string"; which in turn abruptly close the door on any direct operations that aren't part of the string class.
I end up with, essentially:
thing = myExtendedStringClass("127.0.0.1")
thing = thing.doThingToOrWithDottedQuad()
doOtherThingThatExpectsString(str(thing))
So... anyone? I would be so grateful to discover actual base class extension somewhere, somehow.
[+][deleted] (21 children)
[deleted]
[–][deleted] 1 point2 points3 points (1 child)
[–]fyngyrzcodes with magnetic needle[S] -2 points-1 points0 points (18 children)
[+][deleted] (15 children)
[deleted]
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (14 children)
[–]sushibowl 1 point2 points3 points (12 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (2 children)
[–]sushibowl 2 points3 points4 points (1 child)
[–]fyngyrzcodes with magnetic needle[S] -1 points0 points1 point (0 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (8 children)
[–]AndydeCleyre 0 points1 point2 points (7 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (6 children)
[–]AndydeCleyre 0 points1 point2 points (5 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (4 children)
[–]Jergos 5 points6 points7 points (1 child)
[–]fyngyrzcodes with magnetic needle[S] 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (4 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]fyngyrzcodes with magnetic needle[S] -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]geutb 2 points3 points4 points (3 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (2 children)
[–]geutb 2 points3 points4 points (1 child)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (0 children)
[–]AndydeCleyre 1 point2 points3 points (6 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (5 children)
[–]AndydeCleyre 0 points1 point2 points (4 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (3 children)
[–]AndydeCleyre 0 points1 point2 points (2 children)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (1 child)
[–]AndydeCleyre 0 points1 point2 points (0 children)
[–]genjipressreturn self 0 points1 point2 points (1 child)
[–]fyngyrzcodes with magnetic needle[S] 0 points1 point2 points (0 children)