News for hackers. Stuff that matters.
Re:Python 3.7: Introducing Data Classes by ianamartin on Wednesday April 18, 2018 @12:14PM (#16868532)
I'm piqued by one thing in this article. Using an object as a dictionary key. What's the use for that? I don't think it's ever occurred to me to do that.
Re:Python 3.7: Introducing Data Classes by talltimtom on Wednesday April 18, 2018 @12:21PM (#16868604)
Anything hashable can be a dict key, so if you have some mapping between one thing that’s hashable and other things, you can use a dict directly rather than having to come up with an index and keep track of that association.
Re:Python 3.7: Introducing Data Classes by UncleEntity on Wednesday April 18, 2018 @07:53PM (#16872293)
Dynamic dispatching comes to mind.

  d = {Foo : do_something_with_foo,
       Bar : do_something_with_bar}

  d.get(type(x), default_function)(x)
Probably some more efficient way but honestly I've used this before instead of writing a whole if elif chain.

--edit--

Makes python's lack of a switch operator sometimes less painful...

Re:Python 3.7: Introducing Data Classes by ianamartin on Thursday April 19, 2018 @02:14AM (#16873712)
Oh, that makes sense. Not sure why this question rubbed people the wrong way. I was genuinely curious about when you would use an object as a key.

I guess my uses for dictionaries are pretty vanilla. But that’s a kind of thing I do all the time. I set up dictionaries where the key is a possible value from some operation or query, and the value is the function I want to perform on that value.

This extends that concept. Thanks!

Re:Python 3.7: Introducing Data Classes by chubot on Wednesday April 18, 2018 @12:32PM (#16868718)
I'm not sure what part of the article you refer to, but:

- If the objects are value types (as dataclasses are, since they respect equality): it makes sense to use them as dict keys, just as it makes sense to use tuples as dict keys. Changing a tuple to a namedtuple is natural.

- If the objects have reference semantics rather than value semantics, you can still use them as dict keys in Python. The default __hash__ is the id/address of the object (the default __eq__ uses id() too). I don't particularly like this style but I have seen it used. It feels error-prone to me and not "algebraic".

Re:Python 3.7: Introducing Data Classes by comnetxr on Wednesday April 18, 2018 @05:37PM (#16871452)
memoization is a common use case. If you are doing an expensive computation that gives the same answer for the the same inputs, you can build a lookup table for cases you have already encountered to avoid recomputing. you can use an object to encapsulate the inputs (or the inputs might be objects already).
Re:Python 3.7: Introducing Data Classes by dagw on Wednesday April 18, 2018 @05:13PM (#16871242)
Using an object as a dictionary key. What's the use for that?

I've used tuples has dict keys several times, so I don't see why I conceivably wouldn't use a Data Class containing the same data as a dict key.

Re:Python 3.7: Introducing Data Classes by cakebrewery on Wednesday April 18, 2018 @02:09PM (#16869520)
Avoids the extra step of hashing the object to make the key by simply passing the object itself.