[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-11 Thread Rob Cliffe via Python-Dev
You are absolutely right, of course.  It was a wild idea, and a bad one. I find myself moving towards supporting the OP.  I can't see anything terrible about the hash of None always being 0, or perhaps better some other arbitrary constant. Rob On 04/12/2022 03:20, Steven D'Aprano wrote: On

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-09 Thread Rob Cliffe via Python-Dev
You're right of course.  Oh well, it *was* a wild idea. Rob Cliffe On 04/12/2 On 04/12/2022 18:16, Chris Angelico wrote: On Mon, 5 Dec 2022 at 05:11, Rob Cliffe via Python-Dev wrote: Wild suggestion: Make None.__hash__ writable. E.g. None.__hash__ = lambda : 0 # Currently raises

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-04 Thread Steven D'Aprano
On Thu, Dec 01, 2022 at 10:18:49PM +, Rob Cliffe via Python-Dev wrote: > Wild suggestion: >     Make None.__hash__ writable. > E.g. >     None.__hash__ = lambda : 0 # Currently raises AttributeError: > 'NoneType' object attribute '__hash__' is read-only You would have to write to

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-04 Thread Chris Angelico
On Mon, 5 Dec 2022 at 05:11, Rob Cliffe via Python-Dev wrote: > > Wild suggestion: > Make None.__hash__ writable. > E.g. > None.__hash__ = lambda : 0 # Currently raises AttributeError: > 'NoneType' object attribute '__hash__' is read-only Hashes have to be stable. If you change the

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-04 Thread Rob Cliffe via Python-Dev
Wild suggestion:     Make None.__hash__ writable. E.g.     None.__hash__ = lambda : 0 # Currently raises AttributeError: 'NoneType' object attribute '__hash__' is read-only Best wishes Rob Cliffe On 01/12/2022 11:02, Oscar Benjamin wrote: On Thu, 1 Dec 2022 at 06:56, Chris Angelico wrote:

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-03 Thread Carl Friedrich Bolz-Tereick
On 29/11/2022 00:51, Guido van Rossum wrote: To stir up some more fire, I would personally be fine with sets having the same ordering guarantees as dicts, *IF* it can be done without performance degradations. So far nobody has come up with a way to ensure that. "Sets weren't meant to be

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-03 Thread Paul Moore
On Sat, 3 Dec 2022 at 14:46, Yoni Lavi wrote: > > I think this is over-complicating things. I think the key merit of your > > original proposal was its simplicity. Proposing more complicated ways of > > getting the result you want is (IMO) unlikely to succeed, and is only > > likely to cause

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-03 Thread Yoni Lavi
> I think this is over-complicating things. I think the key merit of your > original proposal was its simplicity. Proposing more complicated ways of > getting the result you want is (IMO) unlikely to succeed, and is only > likely to cause people to become even more entrenched in their positions. >

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-03 Thread Paul Moore
On Sat, 3 Dec 2022 at 10:57, Yoni Lavi wrote: > There's a number of Core devs that have taken strong positions against > this change, citing various reasons ranging from "the addition of a > function that returns a constant will cause bloat in the interpreter / > needs to be tested / etc" to

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-03 Thread Yoni Lavi
There's a number of Core devs that have taken strong positions against this change, citing various reasons ranging from "the addition of a function that returns a constant will cause bloat in the interpreter / needs to be tested / etc" to "what you really mean to ask for is set iteration

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-02 Thread Eric Snow
On Mon, Nov 28, 2022 at 6:45 PM Steven D'Aprano wrote: > On Tue, Nov 29, 2022 at 01:34:54PM +1300, Greg Ewing wrote: > > I got the impression that there were some internal language reasons > > to want stable dicts, e.g. so that the class dict passed to __prepare__ > > preserves the order in which

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-02 Thread Terry Reedy
On 11/30/2022 8:48 PM, Rob Cliffe via Python-Dev wrote: Thank you for this very clear analysis, Oscar. It seems to me that this strengthens the OP's case.  I am curious as to whether others agree. I do. On 30/11/2022 13:35, Oscar Benjamin wrote: On Tue, 29 Nov 2022 at 23:46, Steven D'Aprano

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-02 Thread Rob Cliffe via Python-Dev
Thank you for this very clear analysis, Oscar. It seems to me that this strengthens the OP's case.  I am curious as to whether others agree. Best wishes Rob Cliffe On 30/11/2022 13:35, Oscar Benjamin wrote: On Tue, 29 Nov 2022 at 23:46, Steven D'Aprano wrote: On Tue, Nov 29, 2022 at

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-12-01 Thread Oscar Benjamin
On Thu, 1 Dec 2022 at 06:56, Chris Angelico wrote: > > On Thu, 1 Dec 2022 at 17:26, Yoni Lavi wrote: > > > > So it's not like it's even possible to require this generally for all > > objects. > > Well, I mean, in theory you could require that objects whose hash > isn't otherwise defined get

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-30 Thread Yoni Lavi
> Whether determinism is fundamentally good or fundamentally bad depends > heavily on context. Agreed 100%. Unfortunately in Python, you cannot choose your hashing function depending on context. Also, once you've decided to violate determinism somewhere, it's gone. There is no way, in the

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-30 Thread Chris Angelico
On Thu, 1 Dec 2022 at 17:26, Yoni Lavi wrote: > > > the language makes no guarantee about hash consistency between > executions > > because it's futile in the general case, even if objects were to get a serial > `id` and hash by it for example, any change in the number of objects created >

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-30 Thread Yoni Lavi
> the language makes no guarantee about hash consistency between executions because it's futile in the general case, even if objects were to get a serial `id` and hash by it for example, any change in the number of objects created across all of Python (including its builtin modules and various

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-30 Thread Brett Cannon
On Tue, Nov 29, 2022 at 12:58 PM Yoni Lavi wrote: > It does make your argument invalid though, It makes that single sentence invalid, but the rest of my points still hold, e.g. the language makes no guarantee about hash consistency between executions, set order is not guaranteed, etc. are all

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-30 Thread Oscar Benjamin
On Tue, 29 Nov 2022 at 23:46, Steven D'Aprano wrote: > > On Tue, Nov 29, 2022 at 08:51:09PM -, Yoni Lavi wrote: > > > It does make your argument invalid though, since it's based on this > > assumption that I was asking for a requirement on iteration order > > (e.g. like dict's iteration order

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Yoni Lavi
I stand by what I said, there is absolutely nothing disingenious about it. > Over on the Discuss threads, > you have made it clear that the primary reason why you want hash(None) > to return a constant value is so that set iteration order will be > consistent from one run to another. No, it's

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Chris Angelico
On Wed, 30 Nov 2022 at 10:48, Steven D'Aprano wrote: > Let's consider a thought-experiment: suppose we agree to your proposal > to make hash(None) return a constant, but at the same time modify the > set iteration algorithm so that it starts from a different position each > time you iterate,

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Christopher Barker
> it [randomising iteration order or sets] would also break the invariant that `repr(data) == repr(data)` but it is times like this that I feel that it would be worth it. But it wouldn't -- equality of sets doesn't depend on iteration order. And even if you are talking about other types (this

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Steven D'Aprano
On Tue, Nov 29, 2022 at 08:51:09PM -, Yoni Lavi wrote: > It does make your argument invalid though, since it's based on this > assumption that I was asking for a requirement on iteration order > (e.g. like dict's iteration order = insertion order guarantee), which > is not the case. Yoni,

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Yoni Lavi
It does make your argument invalid though, since it's based on this assumption that I was asking for a requirement on iteration order (e.g. like dict's iteration order = insertion order guarantee), which is not the case. Again, determinism means that given all input data and commands fed to a

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Brett Cannon
On Mon, Nov 28, 2022 at 5:38 PM Steven D'Aprano wrote: > On Mon, Nov 28, 2022 at 11:13:34PM +, Oscar Benjamin wrote: > > On Mon, 28 Nov 2022 at 22:56, Brett Cannon wrote: > > > > That's actually by design. Sets are not meant to be deterministic > > > conceptually as they are essentially a

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Yoni Lavi
Looks like it's just miscommunication. There is the original proposal I made, strictly about how None is ought to be hashed, and then there is the separate topic of changing the stability properties of iteration on sets, and whether that can be made with/without a performance regression...

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-29 Thread Yoni Lavi
> I can't see any, but then I couldn't see the security consequences of > predictable string hashes until they were pointed out to me. So it would > be really good to have some security experts comment on whether this is > safe or not. I can't either. I can point out that the complexity attack

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Guido van Rossum
[Oscar Benjamin] > (If you think that there might be a > performance penalty then you haven't understood the suggestion!) Then I don't understand the question, and I will refrain from participating further in this discussion. -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Steven D'Aprano
On Tue, Nov 29, 2022 at 02:07:34AM +, Oscar Benjamin wrote: > Let's split this into two separate questions: Let's not. Your first question about non-deterministic set order being "innately good" is a straw man: as we've already discussed, set order is not non-deterministic (except in the

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Chris Angelico
On Tue, 29 Nov 2022 at 13:12, Oscar Benjamin wrote: > As for point 2. the fact that sets are currently non-deterministic is > actually a relatively new thing in Python. Before hash-randomisation > set and dict order *was* deterministic but with an arbitrary order. > That was only changed because

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Oscar Benjamin
On Tue, 29 Nov 2022 at 01:33, Steven D'Aprano wrote: > > On Mon, Nov 28, 2022 at 11:13:34PM +, Oscar Benjamin wrote: > > On Mon, 28 Nov 2022 at 22:56, Brett Cannon wrote: > > As I understand it, we could make sets ordered, but only at the cost of > space (much more memory) or time (slower)

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Steven D'Aprano
On Tue, Nov 29, 2022 at 01:34:54PM +1300, Greg Ewing wrote: > I got the impression that there were some internal language reasons > to want stable dicts, e.g. so that the class dict passed to __prepare__ > preserves the order in which names are assigned in the class body. Are > there any such use

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Matthias Görgens
For what it's worth, as a user of the language I would like sets to behave as much as possible as-if they were basically dicts that map all elements to `()`. That way I'd have to keep one less mental model in my head. I deliberately say 'as-if' because when I'm a user of the language, I don't

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Steven D'Aprano
On Mon, Nov 28, 2022 at 11:13:34PM +, Oscar Benjamin wrote: > On Mon, 28 Nov 2022 at 22:56, Brett Cannon wrote: > > That's actually by design. Sets are not meant to be deterministic > > conceptually as they are essentially a bag of stuff. If you want > > deterministic ordering you should

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Guido van Rossum
Nah, `__prepare__` very much predates stable dicts and that problem was solved differently. On Mon, Nov 28, 2022 at 4:46 PM Greg Ewing wrote: > On 29/11/22 12:51 pm, Guido van Rossum wrote: > > "Sets weren't meant to be deterministic" sounds like a remnant of > > the old philosophy, where we

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Greg Ewing
On 29/11/22 12:51 pm, Guido van Rossum wrote: "Sets weren't meant to be deterministic" sounds like a remnant of the old philosophy, where we said the same about dicts -- until they became deterministic without slowing down, and then everybody loved it. I got the impression that there were

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Guido van Rossum
To stir up some more fire, I would personally be fine with sets having the same ordering guarantees as dicts, *IF* it can be done without performance degradations. So far nobody has come up with a way to ensure that. "Sets weren't meant to be deterministic" sounds like a remnant of the old

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Oscar Benjamin
On Mon, 28 Nov 2022 at 22:56, Brett Cannon wrote: > > On Sun, Nov 27, 2022 at 11:36 AM Yoni Lavi wrote: >> >> All it takes is for your program to compute a set somewhere with affected >> keys, and iterate on it - and determinism is lost. > > That's actually by design. Sets are not meant to be

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Chris Angelico
On Tue, 29 Nov 2022 at 09:51, Brett Cannon wrote: > ... we worked hard to stop people from relying on consistent > hashing/iteration from random-access data structures like dict and set. > Say what? Who's been working hard to stop people from relying on consistent iteration order for a dict?

[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Brett Cannon
On Sun, Nov 27, 2022 at 11:36 AM Yoni Lavi wrote: > I wrote a doc stating my case here: > > https://docs.google.com/document/d/1et5x5HckTJhUQsz2lcC1avQrgDufXFnHMin7GlI5XPI/edit# > > Briefly, > > 1. The main motivation for it is to allow users to get a predictable > result on a given input (for