Because Python makes no guarantees about string identity, that "abcd" you are using as a sentinel may be the same object (not equal to, the same object) as an "abcd" string the user provided as a valid value.
If that's the sentinel, and the user chose to supply it, why is that a problem? That's the intellectual argument I'm trying to tease out. If you want a sentinel that reads as "<NotGiven>" and the user is allowed to read that in debug output, then why aren't they allowed to pass it as an argument? Is it a typing thing or what? Or is the real world use of nicer reprs actually spurious precisely because third party libraries can implement their own trivially such as by Werkzeug?
Furthermore on the copying thing, ast.literal_eval("<NotGiven>") seems like an excellent outcome.
No one is forcing you to use anything.
But someone is posting links to a discussion forum. And you didn't address the fact that I highlighted - adding stdlib modules is tacit endorsement as the one obvious way - discuss.
If that's the sentinel, and the user chose to supply it, why is that a problem? [...] If you want a sentinel that reads as "<NotGiven>" and the user is allowed to read that in debug output, then why aren't they allowed to pass it as an argument?
The user doesn't supply the sentinel, that's set when you write the function. The user supplies the default value.
The sentinel can read "<NotGiven>" in the debug output; However, it can't be the "<NotGiven>" string; that is, it can, but because of the identity thing mentioned before, you won't be able to tell it apart from the sentinel.*
You can restrict the valid values to exclude "<NotGiven>". In your own code you may be able to guarantee it's never a valid value. But as a library author, you can never foresee all the use cases – maybe there's a crazy user out there that needs to use "<NotGiven>". And you'd have to document it, and the users would always need to be aware of it (which isn't really friendly, IMHO).
* (the identity thing, with more details)
Due to string (possible) interning, subsequent ast.literal_eval("'<NotGiven>'") calls may result in the same object; or not. (ast.literal_eval("<NotGiven>") (no quotes) does not result in a valid value, because <NotGiven> is not a valid literal.)
Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed.
View all comments
Show parent comment
1
u/MarsupialMole Jun 07 '21
If that's the sentinel, and the user chose to supply it, why is that a problem? That's the intellectual argument I'm trying to tease out. If you want a sentinel that reads as "<NotGiven>" and the user is allowed to read that in debug output, then why aren't they allowed to pass it as an argument? Is it a typing thing or what? Or is the real world use of nicer reprs actually spurious precisely because third party libraries can implement their own trivially such as by Werkzeug?
Furthermore on the copying thing,
ast.literal_eval("<NotGiven>")seems like an excellent outcome.But someone is posting links to a discussion forum. And you didn't address the fact that I highlighted - adding stdlib modules is tacit endorsement as the one obvious way - discuss.