Live data from Hacker News

Don't let dicts spoil your code

roman.pt

21–30 of 121 posts

Re: Don't let dicts spoil your code

#21

For better or for worse, Python doesn't do typing well. I don't disagree that I prefer well defined types, but if that is your desire then I think Python is perhaps not the correct choice of language.

> Python doesn't do typing well

Golang does typing, but JSONs are PITA to handle.

Try parsing something like `[{"a': 1, "b": "c", "d": [], "e": {}}, null, 1, "2"]` in go.

Types are a bless as well as a curse.

Re: Don't let dicts spoil your code

#23

Here’s an out-there take, but one I’ve held loosely for a long time and haven’t shed yet: dicts are not appropriate for what people mostly use them for, which is named access to member attributes. dict is an implementation of a hash table. Hash table are designed for o(1) lookup of items. As such, they are arrays which are much bigger than the number of items they store, to allow hashing items into integers and sides…

NamedTuples are great, but they let you do too much with the objects. You probably don't want users of your GitHubRepo class to be able to do things like `repo[1]` or `for foo in repo`. Dataclasses have more constrained semantics, so I reach for them by default. In my ideal world they would default to frozen=True, kw_only=True, slots=True, but even without those they're a big improvement.

Re: Don't let dicts spoil your code

#24
post #6

Less important in Elixir (where they are "maps") due to the immutable nature of them as well as the Struct type which is a structured map.

Yup! I find Elixir makes it really intuitive to know when to represent a collection as a map and when to use a list of tuples. And its easy to transform between the two when needed.

Re: Don't let dicts spoil your code

#25
post #22

dicts are OK, because at least they do have a `key` and it does mean something. un-annotated tuples and too many func params are cancer.

No no,

Un-annotated tuples and too many func params are OK, because at least they are pushed and popped from the stack.

Calls and rets without a prologue and epilogue on the other hand…

Re: Don't let dicts spoil your code

#26
post #21

For better or for worse, Python doesn't do typing well. I don't disagree that I prefer well defined types, but if that is your desire then I think Python is perhaps not the correct choice of language.

> Python doesn't do typing well Golang does typing, but JSONs are PITA to handle. Try parsing something like `[{"a': 1, "b": "c", "d": [], "e": {}}, null, 1, "2"]` in go. Types are a bless as well as a curse.

Thats only because your list has different types. Its a badly formed API and if you really need to support that use case then you can use maps and reflection to handle it.

Re: Don't let dicts spoil your code

#28
post #22

dicts are OK, because at least they do have a `key` and it does mean something. un-annotated tuples and too many func params are cancer.

No no, Un-annotated tuples and too many func params are OK, because at least they are pushed and popped from the stack. Calls and rets without a prologue and epilogue on the other hand…

> from the stack

Or many, many stacks you can't comprehend nor amend.

I dare to add a new `key` to a dict, can you modify a func call or a tuple with confidence?

Re: Don't let dicts spoil your code

#29

For better or for worse, Python doesn't do typing well. I don't disagree that I prefer well defined types, but if that is your desire then I think Python is perhaps not the correct choice of language.

Personnaly I became a huge fan of beartype : https://pypi.org/project/beartype/

Leyec, the magic dev behind it managed to make a full python type checker with super advanced features and about 0 overhead. It's crazy

Post reply on HN