News for hackers. Stuff that matters.
Re:Python overtakes JavaScript as most questioned language o... by sametmax on Saturday April 20, 2019 @05:14AM (#19705531)
I often say that Python is the best at nothing, but pretty good at most things. And hence it became huge by the sheer appeal of being versatile. Life is short and most people needing to program are not programmers. Also many programmers didn't start as such, and were simply looking for an easy way to solve a task.
Re:Python overtakes JavaScript as most questioned language o... by roenxi on Saturday April 20, 2019 @06:43AM (#19705798)
There is a bit more insight to be found by reversing that - Python is simpler than other programming languages and achieves that by sacrificing a little power.

Compare it to C, which it has displaced as an introductory language. Python and C are superficially interchangeable to a beginner, but to learn C you have to invest time into pointers and memory management. Python sacrifices the efficiency of direct memory management to avoid the complication of pointers. Arguably, this is a significant benefit because it also cuts down on ways to introduce bugs.

Compare it to a Lisp - Python has a pretty inferior set of basic capabilities (can't even use a dict as a dict key, drives me mad), but that just means there is only one easy way to do things that everyone uses. Less room to think up clever ways of doing things, less complexity reading other's code.

Being the 'best' language for a task has a huge cost - incidental complexity created by including specialist constructs. Python's success suggests maybe programmers prefer this complexity to be encapsulated in a library rather than the language core.

Re:Python overtakes JavaScript as most questioned language o... by np_tedious on Saturday April 20, 2019 @07:51AM (#19706030)
> can't even use a dict as a dict key

Curious, is this actually something you want to do often? While there is no hashable+immutable frozendict ala frozenset, you could throw one together or even define a hash function on a subclass and "swim at your own risk" not to mutate it later.

I've encountered this enough with lists and the obvious solution is to cast them to tuples. Can't say I've found the same with dicts tho.

As for the comparison with lisp - in most lisps this is a non issue because nearly all structures are immutable

Re:Python overtakes JavaScript as most questioned language o... by roenxi on Saturday April 20, 2019 @08:32AM (#19706188)
Absolutely. For me it is a 50-50 chance whether I have data in a dict that needs to be mapped to something or have data in a list/tuple that needs mapping.

Many major programming languages undersell how natural key-value data structures are; usually by providing unwieldy mapping data types. Clojure has spoiled me.

> you could throw one together

Yeah, I do. Most Python programmers wouldn't see a need for that, but that can be attributed to Python not having the capability by default and so they've just internalised not to use dict for anything that needs to be a key. World's mildest form of learned helplessness, I suppose.

Re:Python overtakes JavaScript as most questioned language o... by goodside on Saturday April 20, 2019 @10:10AM (#19706586)
> Most Python programmers wouldn't see a need for that, but that can be attributed to Python not having the capability by default and so they've just internalised not to use dict for anything that needs to be a key.

You're being extremely uncharitable here.

To a Python programmer "using a dict as a key" is a nonsensical idea for the same reason "using a list as a key" is — both dicts and lists are mutable. Python programmers don't think of dicts or lists as synonymous with their content.

I'm assuming you don't want to literally use a dict as a key, you want to use the keys and values contained in a dict as a key. Nothing stops you. You just have to say it:

    point = dict(x=4, y=5, z=1)
    point_reviews = dict()
    point_reviews[tuple(point.items())] = "A+++ great coord would point to again"
You might wonder why you can't just use dicts directly. Again, dicts are mutable. What happens if you write:

    point = dict(x=0, y=9001)
    point_reviews = dict()
    point_reviews[point] = "Love how it's over 9000 here!"
    point["y"] = 8999
    result = point_reviews[dict(x=0, y=8999)]  # Is this a KeyError or not?
More detail on this issue is explained here: https://wiki.python.org/moin/DictionaryKeys

In the simplest case where you have a predefined list of keys, I'd suggest using a namedtuple instead of a dict. If you need to treat a dict's content as hashable but can't enumerate the keys, `tuple(d.items())` is probably the best choice in recent Python versions. Be aware though that in older Pythons you can't rely on stable sort order of dictionary items, so you have to use something like `frozenset(d.items())`.

Re:Python overtakes JavaScript as most questioned language o... by sago on Saturday April 20, 2019 @11:08AM (#19706901)
Good information, and well put. A small quibble: you should probably almost never use tuple(), even on recent Python:

    >>> tuple(dict(a=1, b=2).items())
    (('a', 1), ('b', 2))
    >>> tuple(dict(b=2, a=1).items())
    (('b', 2), ('a', 1))
Of course, if you can absolutely guarantee how the dictionary has been constructed, it is possible. No rules are absolute. But in my experience, frozenset() is probably better:

    >>> frozenset(dict(a=1, b=2).items())
    frozenset({('b', 2), ('a', 1)})
    >>> frozenset(dict(b=2, a=1).items())
    frozenset({('b', 2), ('a', 1)})
(Or sometimes even id(), as, if you are confident enough that the dictionary is identical, that might be because it came from the same place. If you are building a reverse dictionary, say, or a cache of indices. Though obviously that it is much more situationally dependent.)
Re:Python overtakes JavaScript as most questioned language o... by Scarblac on Saturday April 20, 2019 @11:44AM (#19707124)
Well. Now that dictionaries remember their order in newer Python versions, there is a case to be made that dict(a=1, b=2) is not the same dictionary as dict(b=2, a1). So they should be different dict keys, if they could be dict keys.
Re:Python overtakes JavaScript as most questioned language o... by Nullabillity on Saturday April 20, 2019 @05:45PM (#19709052)

    dict(a=1,b=2) == dict(b=2,a=1)
is True (on 3.7.3). I'm not too keen on introducing a third type of equality on the same type, even if it makes some theoretical sense.
Re:Python overtakes JavaScript as most questioned language o... by Scarblac on Saturday April 20, 2019 @11:42AM (#19707109)
And worse, the dict's values may themselves be mutable. So even tuple(point.items()) is not a sensible dict key in the general case.
Re:Python overtakes JavaScript as most questioned language o... by sametmax on Saturday April 20, 2019 @12:43PM (#19707480)
id(dictionary) is the key you want: immutable, reliable, cheap.
Re:Python overtakes JavaScript as most questioned language o... by goodside on Saturday April 20, 2019 @03:21PM (#19708377)
That value will not change when the contents of the dict are updated. You’re keying on the dict itself rather than the hashed contents. That’s what you want in some situations, but it’s not what the parent post is looking for.
Re:Python overtakes JavaScript as most questioned language o... by Nullabillity on Saturday April 20, 2019 @05:49PM (#19709072)
It's also not reliable, since the same address can be reused if the dict is GCed. This is actually really easy to induce in Python, since it uses immediate refcounting. In my REPL (3.7.3):

    >>> a = {}
    >>> x = id(a)
    >>> del a
    >>> a = {}
    >>> y = id(a)
    >>> x == y
    True
Re:Python overtakes JavaScript as most questioned language o... by samatman on Saturday April 20, 2019 @08:47AM (#19706250)
Several times, when working with Lua, I’ve made what I call “Janus tables”.

Give them a string, they return a table or function. Give them the table or function, they return a string.

It’s a useful technique that you can only apply if your hashmap can map anything to anything.

Re:Python overtakes JavaScript as most questioned language o... by BurningFrog on Saturday April 20, 2019 @10:23AM (#19706634)
https://en.wikipedia.org/wiki/Bidirectional_map

Any hashmap will have problems with storing mutable objects, where the hash value itself can change after the object has been stored, causing some very unwanted paradoxes.

And that is the problem with Python dicts as keys.

Re:Python overtakes JavaScript as most questioned language o... by marcianx on Saturday April 20, 2019 @10:56AM (#19706830)
I don't think mutability is a concern if your goal is to get a bidi map between strings and (mutable) _instances_; i.e. if you just want to use the reference to the dict itself as the hash rather than the contents of the dict itself.
Re:Python overtakes JavaScript as most questioned language o... by BurningFrog on Saturday April 20, 2019 @11:39AM (#19707089)
Yeah, I don't actually know if Python computes hashes for dicts by content or "identity".

I just assumed it was the former, based on the fact that dicts are disallowed as keys.

Re:Python overtakes JavaScript as most questioned language o... by fiddlerwoaroof on Saturday April 20, 2019 @01:26PM (#19707689)
It’s been a while, but I’m pretty sure the reason why a dictionary can’t have a dictionary as it’s key is because the dictionary class doesn’t implement the __hash__ method that would compute the hashcode.
Re:Python overtakes JavaScript as most questioned language o... by np_tedious on Sunday April 21, 2019 @06:34AM (#19711464)
This is definitely the proximate reason.

The reason for that choice is because the object is mutable.

Re:Python overtakes JavaScript as most questioned language o... by fiddlerwoaroof on Sunday April 21, 2019 @02:39PM (#19713724)
Sure, but as others have mentioned, you can make your own choice by subclassing dict and defining your own hash function, as long as that function is defined in a way that follows the appropriate rules.
Re:Python overtakes JavaScript as most questioned language o... by CydeWeys on Saturday April 20, 2019 @10:08AM (#19706576)
What's a use case for needing to be able to do this?
Re:Python overtakes JavaScript as most questioned language o... by iainmerrick on Saturday April 20, 2019 @07:56AM (#19706053)
Not often, but when you do want to do it, it’s frustrating (and unusual for Python) that the natural and obvious way doesn’t work.
Re:Python overtakes JavaScript as most questioned language o... by jotaf on Saturday April 20, 2019 @08:21AM (#19706146)
When I read this my first thought was "you should use a FrozenDict", but looking into it, it seems that it doesn't exist; I assumed so because of FrozenSets (hashable sets).

You can still create a hashable key from a dict with FrozenSet(mydict.items()).

Re:Python overtakes JavaScript as most questioned language o... by np_tedious on Saturday April 20, 2019 @08:24AM (#19706159)
Ah nice. Yes that last suggestion is probably a more simple and elegant approach than anything I'd mentioned / thought about
Re:Python overtakes JavaScript as most questioned language o... by typon on Saturday April 20, 2019 @08:49AM (#19706254)
You don't really need a FrozenSet if you're already willing to iterate over this dictionary. You can just use a tuple, which is also hashable.

tuple(d.items())

Re:Python overtakes JavaScript as most questioned language o... by detaro on Saturday April 20, 2019 @08:50AM (#19706260)
That depends on the order in which the values are are returned by items(), which you likely do not want.

    >>> tuple({1:2,3:4}.items()) == tuple({3:4,1:2}.items())
    False
    >>> frozenset({1:2,3:4}.items()) == frozenset({3:4,1:2}.items())
    True
Re:Python overtakes JavaScript as most questioned language o... by typon on Saturday April 20, 2019 @10:09AM (#19706582)
Good point
Re:Python overtakes JavaScript as most questioned language o... by ken on Saturday April 20, 2019 @11:44AM (#19707125)
What Lisps are you thinking of? In Common Lisp, Scheme, and Emacs Lisp, at least (IIRC), all composite data structures (strings, vectors, etc) are mutable, or can be. The only Lisp I can think of in which "nearly all structures are immutable" is Clojure.
Re:Python overtakes JavaScript as most questioned language o... by PopeDotNinja on Saturday April 20, 2019 @09:49AM (#19706511)
Python probably has a set type, but a common general use case for use an associative array as a key is when making a set.

dict = { "foo" : "bar" }

set = { dict : true }

set.has_key?(dict) # true

I'm sure there are other use cases where dict keys can be convenient.

Re:Python overtakes JavaScript as most questioned language o... by Scarblac on Saturday April 20, 2019 @11:49AM (#19707157)
Yes, Python has both a set type and the immutable "frozenset" type, that can be used as a dictionary key.

someset = {"one", "two", "three"}

"one" in someset # true

frozen = frozenset(someset)

somedict = {frozen: "example"}

frozen in somedict # true somedict[frozen] # "example"

Re:Python overtakes JavaScript as most questioned language o... by ensignavenger on Saturday April 20, 2019 @02:52PM (#19708223)
> Can't say I've found the same with dicts tho

I would think a NamedTuple would do the trick? (https://docs.python.org/3/library/collections.html#collectio...)

Re:Python overtakes JavaScript as most questioned language o... by crdoconnor on Saturday April 20, 2019 @06:06AM (#19705682)
Good C bindings and a willingness to accept that it wasn't ever going to be good at doing low level fast code are an underrated facet of how python grew so popular.

Golang, by contrast, is really quite schizophrenic sometimes about whether it wants to be a high or low level language.

Re:Python overtakes JavaScript as most questioned language o... by blunte on Saturday April 20, 2019 @05:40AM (#19705614)
Bingo. When you just need to get something done, Python is great. Some quick googling to find the right libraries for the task, and then some caveperson programming, and the deal is done.

Outside of lisps, I find Python to be one of the simplest syntaxes, so the only effort is the logic to solve the problem.

Re:Python overtakes JavaScript as most questioned language o... by AtHeartEngineer on Saturday April 20, 2019 @11:24AM (#19707006)
I've been programming since I was a teenager, and for a long time I found myself struggling with my syntax and edge cases. Once I started using python, about 10 years ago, programming quickly turned into "what's the best way to solve this problem", instead of "wtf do you mean null pointer".
Re:Python overtakes JavaScript as most questioned language o... by petre on Saturday April 20, 2019 @12:25PM (#19707379)
The feeling is quite the same when switching from C to D. You don't have to use the advanced features of the language, but they're there if you want them.
Re:Python overtakes JavaScript as most questioned language o... by judge2020 on Saturday April 20, 2019 @01:52PM (#19707841)
Jack of all trades, master of none

but often better than master of one