News for hackers. Stuff that matters.
Re:Python overtakes JavaScript as most questioned language o... by z3t4 on Saturday April 20, 2019 @04:38AM (#19705451)
Seems like the most difficult programming is no longer cache invalidation and naming things. Its now about strings, and to duplicate arrays ... The stats are skewed because hordes of beginners. Mainly driven by what schools teaches and what languages has the most hype. That said Python is huge . Damn you significant white space.
Re:Python overtakes JavaScript as most questioned language o... by Spivak on Saturday April 20, 2019 @12:19PM (#19707332)
I'm sure there's lots of beginners to programming but most of the time when I find myself asking basic questions like "how to duplicate array Python" isn't because I couldn't spend 2 minutes writing a function to do it but because there already exists a generally accepted best way to perform this operation that leverages the standard library and there is seemingly no other way to discover what that is.

Like for this specific example:

https://stackoverflow.com/questions/2612802/how-to-clone-or-...

Knowing the right thing to do requires you be informed about the history of Python language design. Like you could just read the documentation and see that lists have a copy method but it doesn't give you a good grasp of what the alternatives are and why you might choose them. And prior to Python 3.3 you're expected to know that the best practice way to do this is to leverage an edge case in the slicing syntax or the list function. Doesn't matter if you're the best computer scientist in the world, sifting through the massive corpous of documentation and historical knowledge required to write good Python is nigh impossible unless you're willing to dedicate yourself Python.

This is something that languages need to tackle because the gap between doing a thing and doing that same thing in a way that follows language conventions and best practices is too damn high.

Re:Python overtakes JavaScript as most questioned language o... by belorn on Saturday April 20, 2019 @02:37PM (#19708142)
The best way to perform a specific action can sometimes change depending on what method is currently fastest. Copy, slice, instancing a new list object, the copy module, each can have their own code base. It would not be surprise me at all if which one is the fastest method has shifted several times between 2.7 and latest version.

That said, copying is in this case the end result of distinct different methods. In the stackoverflow answer, the only redundant way to copy is the .copy method list has. If I understand right it is just a convenient method in order so new programmers don't need to import the copy module.

list() is a common way to create a new list, and the constructor can take a initial sequence. Lists are sequences, so list can take a list, but that is just a side effect.

Slicing is very useful for taking the first or last objects of a list. if you take all the first and all the last objects, you get a copy. Its not the primary function of slicing, but a useful side effect.

The copy module is a lower level interface to work with python objects. As any other lower level interface it mostly used in exception cases when you really want a "deep copy". I have used that trick once in my whole programming career, and it was to work around a restriction in a third-party module for a corner case which they clearly didn't consider.

Re:Python overtakes JavaScript as most questioned language o... by diminoten on Sunday April 21, 2019 @01:48PM (#19713408)
Python documentation is some of the best of any language, a lot of times you'll be able to find your answer quickly if you just look. The SO questions are almost all answerable with links directly to the docs.

This, above many other things, keeps me coming back to Python year after year.

[dead]
Re:Python overtakes JavaScript as most questioned language o... by mattmanser on Saturday April 20, 2019 @01:57PM (#19707870)
1990 is calling and wants you back... But seriously, many of us simply don't remember that sort of book learning.

If it comes to you naturally, fine, just realise that people are different and have different skills, memorization techniques and care about different things.

Some programmers are like you (a tiny minority afaik) and can read a dry spec or a book and remember all the intricacies and gotchas. It takes a certain type of mind to do that. And I would note that none of the people I knew who could do that were particularly good programmers.

Others, most as far as I've seen in my more than a decade career, learn by making, experimenting and adapting examples.

This is made worse now in some ways because back in the day many editors used to have fantastically well written and edited help, and that has generally disappeared.

Press F1 on a type or function and you'd get reams of well written examples and explanations.

Now most programming languages don't, the documentation is sparse and they simply rely on Stack Overflow and Google to provide all the examples for them.

However, that means that old ways of writing things can stick around as often no-one edits a SO answer to correct it.

Re:Python overtakes JavaScript as most questioned language o... by tempguy9999 on Saturday April 20, 2019 @02:26PM (#19708068)
> many of us simply don't remember that sort of book learning.

You mean you haven't heard of books, or can't read, or can't be bothered? Presumably none of these so what are you saying?

> just realise that people are different and have different skills, memorization techniques

Fine, I agree totally, use those! But here's what the original poster said "generally accepted best way to perform this operation that leverages the standard library and there is seemingly no other way to discover what that is". If you can find the answer on SO, that's fine - you have your answer! But that's not what he said. He said there's no way of finding it. I said it's in books, you say it's on SO, so there clearly is.

> Some programmers are like you and can read a dry spec or a book and remember all the intricacies and gotchas

Which I can't and don't. Please re-read my penultimate sentence where I said it's ok not to, nor was there need to. Kind of twice in fact ("You're there to deliver results correctly to the business, not deliver idiomatically perfect code." -and- "There's NO shame in not crawling into the furthest, darkest corners of a language.")

> And I would note that none of the people I knew who could do that were particularly good programmers

Oh noes, I haz been trolled :)

> Now most programming languages don't,

No language I've ever used ships with a tutorial (oh wait, delphi's did. Rather crap but it was there). Python's online docs are ok (not great, ok). So pick a book. I already recommended one. Reading is the same whether it comes from paper or phosphor. Just don't complain it needs work. It does. Sorry.

From experience: not knowing how your tools work is a recipe for vastly increasing your workload. Reading a book is a boring, tedious shag but will give you the greatest savings of time overall.

> learn by making, experimenting and adapting examples

Yup, that too. Books AND that. You're quite right. I should not have overlooked that.

Re:Python overtakes JavaScript as most questioned language o... by mattmanser on Saturday April 20, 2019 @05:34PM (#19709003)
Sorry, phrased that badly, I meant that being able to reel off tech specs wasn't an indicator of being a good programmer. For example, one guy was pretty good, but another was literally terrible, couldn't actually program, just able to talk about it.
Re:Python overtakes JavaScript as most questioned language o... by ychen306 on Saturday April 20, 2019 @04:15PM (#19708639)
This seems odd. I've known no one who learns programming by reading books. Reading books help, but most people get proficient by working on projects.
Re:Python overtakes JavaScript as most questioned language o... by tempguy9999 on Saturday April 20, 2019 @05:25PM (#19708986)
Hmm, you're right, I agree, but you need to understand a language before working on a project (OK, maybe that's my style, maybe others interlace working + reading).

But the original poster claimed info was just so hard to get and I said it wasn't.

Horses for courses, ok. In reality it's books + SO + lots of online info + your local expert (there's usually one at least, though expertise is relative) + transferable skills from other languages.

All I'm saying is you need that info, and it's there, one way or another. There's so much info now out there compared to when I started programming. It's there, use it.

Re:Python overtakes JavaScript as most questioned language o... by speedplane on Sunday April 21, 2019 @03:53AM (#19711050)
> I've known no one who learns programming by reading books.

Similarly, taking a dance class won't make you a dancer, but it helps.

Re:Python overtakes JavaScript as most questioned language o... by wolco on Sunday April 21, 2019 @03:49AM (#19711038)
I did, years ago before I had a computer and when I got an opportunity I would type programs at school/terminals/store computers. I think being forced to visualize the code before can help because when you sit down it's just typing.
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

Re:Python overtakes JavaScript as most questioned language o... by mikekchar on Saturday April 20, 2019 @11:45PM (#19710429)
I actually use Stack Overflow to look for idiomatic ways to do things in languages that I don't have a lot of experience with. So I literally search for things like how to duplicate arrays, even though I'm quite capable of achieving the required output myself. In fact, I use SO almost exclusively for "Is this a bug with a common workaround" and "Is there a more idiomatic way to do this" questions.
Re:Python overtakes JavaScript as most questioned language o... by chapium on Saturday April 20, 2019 @09:26AM (#19706414)
Any Q&A board is going to run into this problem. It takes a lot more work to become an intermediate or advanced user. Given enough time and popularity a topic will be overwhelmed with beginner q&a.
Re:Python overtakes JavaScript as most questioned language o... by caprese on Saturday April 20, 2019 @08:18AM (#19706139)
protip: you can also increase your StackOverflow clout by merely asking or answering the most generic basic questions.

You'll look like a community expert and thought leader in no time.

Helps the resume inhalers that weigh these arbitrary things.

More complex questions about specific frameworks or how that might work with your ORM and memory cache just don't get the attention.

Re:Python overtakes JavaScript as most questioned language o... by war1025 on Saturday April 20, 2019 @10:05AM (#19706561)
Do people really use StackOverflow for answering job-related programming questions? The only time I found the questions on there useful was when I was a first year student learning programming. Maybe I am just bad at searching / asking.
Re:Python overtakes JavaScript as most questioned language o... by caprese on Saturday April 20, 2019 @10:21AM (#19706630)
I did, you just abstract the problem out.

Some programming language and frameworks have more active communities on StackOverflow than others.

So in my sprints, I would typically have 3 tasks in progress at once: 1 I would work on myself, 1 would go on Stackoverflow waiting for the answer, and 1 would be something that required progress from a different team in the company (like a new API endpoint so I could do something on a frontend, or different service)

It was pretty efficient for me. In some communities, the real thought leaders and framework developers are on stack overflow, watching the hashtag like a hawk.

You can also use github issues on the actual package you have issue with pretty well too, alongside it.

So you're really crowdsourcing a lot, and evaluating if that solution is correct enough for your implementation.

But so many times, especially with frameworks, the answer is a good copy and paste job. your mileage and use case may vary.

Re:Python overtakes JavaScript as most questioned language o... by _slyo on Saturday April 20, 2019 @10:21PM (#19710197)
I graduated 5 years ago and I still use it daily.
Re:Python overtakes JavaScript as most questioned language o... by macspoofing on Saturday April 20, 2019 @08:59AM (#19706298)
StackOverflow/Python version of Eternal September.
Re:Python overtakes JavaScript as most questioned language o... by jononor on Saturday April 20, 2019 @06:44AM (#19705805)
Definitely a lot of beginners, many learning it in school, and many learning programming not as their main thing, but as a tool to use in whatever field they are in. More like MATLAB than Java or C#. But I believe that a fair bit of them will continue to use Python in the future (and get over the beginner problems), as they get jobs, startup ideas, side-projects. Curious to see what effect that has over time.