Re: Idea: Tagged strings in python

On Mon, Dec 19, 2022 at 03:48:01PM -0800, Christopher Barker wrote:
No.
And of course, by default, MyList slices are MyLists too, right? No.
type(MyList(range(5))[1:]) <class 'list'>
This is less of an issue for dicts because there are few dict methods and operators which return dicts. Speaking of dicts, the dict.fromkeys method cooperates with subclasses. That proves that it can be done from a builtin. True, it is a classmethod rather than an instance method, but any instance method can find out its own class by calling `type()` (or the internal, C equivalent) on `self`. Just as we can do from Python.
And that’s just the nature of the beast.
Of course it is not. We can write classes in Python that cooperate with subclasses. The only difference is that builtins are written in C. There is nothing fundamental to C that forces this behaviour. It's a choice. -- Steve

On Tue, 20 Dec 2022 at 11:16, Steven D'Aprano <steve@pearwood.info> wrote:
What you really mean here is that fromkeys cooperates with subclasses *that do not change the signature of __init__*. Otherwise, it won't work. The reason this is much easier with a classmethod alternate constructor is that, if you don't want that behaviour, just don't do that.
So? Just don't use NumDict.fromkeys(), it doesn't make sense for a NumDict. And that's fine. But if something else didn't work, it would cause major problems. Which is why instance methods return plain dictionaries:
type(NumDict(5) | NumDict(3)) <class 'dict'>
How is the vanilla dictionary supposed to know how to construct a NumDict correctly? Come to think of it, how is dict supposed to know how to construct a defaultdict? Oh, it doesn't really.
All it does is construct a vanilla dictionary, because that's all it knows how to do. If the rule is "all operations return an object of the subclass automatically", then the corollary is "all subclasses must retain the signature of the superclass". ChrisA

On Tue, 20 Dec 2022 at 11:16, Steven D'Aprano <steve@pearwood.info> wrote:
What you really mean here is that fromkeys cooperates with subclasses *that do not change the signature of __init__*. Otherwise, it won't work. The reason this is much easier with a classmethod alternate constructor is that, if you don't want that behaviour, just don't do that.
So? Just don't use NumDict.fromkeys(), it doesn't make sense for a NumDict. And that's fine. But if something else didn't work, it would cause major problems. Which is why instance methods return plain dictionaries:
type(NumDict(5) | NumDict(3)) <class 'dict'>
How is the vanilla dictionary supposed to know how to construct a NumDict correctly? Come to think of it, how is dict supposed to know how to construct a defaultdict? Oh, it doesn't really.
All it does is construct a vanilla dictionary, because that's all it knows how to do. If the rule is "all operations return an object of the subclass automatically", then the corollary is "all subclasses must retain the signature of the superclass". ChrisA
participants (2)
-
Chris Angelico
-
Steven D'Aprano