6 ms·
These really aren't idioms. An idiom in natural language is a phrase or saying whose meaning cannot be understood without someone telling you what it's supposed
by c3534l 7y ago
These really aren't idioms. An idiom in natural language is a phrase or saying whose meaning cannot be understood without someone telling you what it's supposed to mean (that is, you can't figure it out just by reading the words).
In programming, an idiom is a common, accepted way of accomplishing a task which you wouldn't figure out to do all on your own and whose meaning isn't obvious to programmers of other languages.
I suppose you could argue that certain idioms are more or less universal: using "i" as the index in a loop isn't obvious even if you know a lot of math (why not use j?).
But no one who doesn't program Python is going to figure out the first thing you do in any new Python project is write the line:
if __name__ == "__main__":
main()
The fact that it's impenetrable if it's not first explained to you that you should do that is what makes it idiomatic.
Idiomatic also means particular to a specific person or group. If your website is based on comparing the same idiom in multiple languages, that's a good indication it's not actually idiomatic. That's probably just a common task.
- SamBam 7y agoYou're correct, and I think "patterns" would be a more straightforward term. I believe the confusion probably stems from the word "idiomatic," which means "using, containing, or denoting expressions that are natural to a native speaker." i.e. "idiomatic English" simply means that it sounds like a native speaker. Likewise, "idiomatic python" would be code that reads like a fluent python programmer.
- afiori 7y agoand to sum it up the "if __name__ == "__main__":" is a standard idiomatic pattern in python
- Dylan16807 7y ago"Idiom" and "idiomatic" are connected. You've just demonstrated that the use is correct.
- SamBam 7y agoOf course they are "connected." I was explaining the confusion. However, "connected" is not the same as meaning the same thing. 1. idiom: a group of words established by usage as having a meaning not deducible from those of the individual words (e.g., rain cats and dogs, see the light ). 2. idiomatic: using, containing, or denoting expressions that are natural to a native speaker. Also "connected" is "idiosyncratic," and that means yet a third thing. "Idiosyncratic programming" would be the opposite of what you want! (Definitions from Oxford University Press / https://www.lexico.com/en https://www.lexico.com/en)
- Dylan16807 7y agoGo down a line or two on the definitions for 'idiom'. > A form of expression natural to a language, person, or group of people. > The dialect of a people or part of a country. I'm sure there could be grammatical nitpicks, but the word choice is appropriate.
- edflsafoiewq 7y agoYeah. "The C++ idiom" makes sense though, so thinking of this as a big matrix with the rows labelled with a task and the columns labelled with a language, rather than calling the rows "idioms", you can call the columns "idioms" instead.
- nlawalker 7y agoTotally agree. Does anyone know of any good language tutorials (for any language) targeted at people who are already familiar with programming that introduce a language through its idioms? I've always thought that would be a really good way to get up to speed on a new language really efficiently. Just that little snippet of Python there could be used to quickly and effectively introduce an experienced non-Python programmer to three or four important ideas in Python.
- waylandsmithers 7y agoThe learn x in y minutes series of sites?
- travisjungroth 7y agoFluent Python is an amazing book focused on teaching idiomatic Python to experienced programmers.
- disgruntledphd2 7y agoFluent Python is an amazing book, even the first chapter made loads of weird python stuff just click for me (len, for example).
- nlawalker 7y agoThanks, this looks like exactly the kind of thing I'm looking for!
- maxk42 7y agohttps://learnxinyminutes.com https://learnxinyminutes.com
- DonaldPShimoda 7y agoThis use of "idiom" is well established outside of natural language. Merriam-Webster gives the following as the third sense of "idiom": > a style or form of artistic expression that is characteristic of an individual, a period or movement, or a medium or instrument So it doesn't have to be inscrutable; just something that's characteristic of a particular way of doing things. The Python community favors list comprehensions over maps and filters, so using list comprehensions is part of writing idiomatic Python even though they are not in themselves impossible to understand.
- c3534l 7y agoI would agree that using list comprehensions over iterating is idiomatic Python, but I would not say that the task of applying a function to a list is an idiom like how the website seems to indicate. There may or may not be an idiomatic way in a given language, it's really dependant on the language which is why you can't map tasks to idioms. C# has Python list comprehensions, but it's not the preferred way to do things and so I wouldn't call it a programming idiom. I'm not trying to argue that programming idioms are the exact same thing as idiomatic expressions, I was trying to explain what programming idioms are by appealing to the sense in which the term was coined.
- dagurp 7y agoTrue, this is more like a "cookbook"
- neilv 7y agoIt looks exactly like what used to probably be called "cookbook". The first cookbook I recall was for Perl, and I later saw other language users try to emulate that, and even use the tasks it identified as a template for their own language's cookbook. I save "idiomatic" for programming linguistic style guidelines that, for many tasks, help select among multiple ways of doing a task (or of larger structuring/approach). For example, in Scheme, tending to avoid mutations, structuring algorithm implementations to use tail calls, using first-class closures, using syntax extension for minilanguages/DSLs, etc. Pythonistas might call their own popular style "Pythonic". There's overlap with cookbook-type stuff. For example, in some language that permits both approaches, maybe the approach of folding over a set of filenames is arguably more idiomatic than the approach of constructing a list of filenames. Consequently, the cookbook example might be of the folding approach. I don't want to call that particular task an idiom, because I find the term useful for a different purpose, but there's overlap. Cookbooks often answer the question "which library and/or calls do I use". If the cookbook spends time on more general tasks, like "coding an algorithm" or "traversing a collection", then the discussion might be more about what's idiomatic in the language. Some of the fuzzy distinction might be analogous to the fuzzy distinction between language and library. Maybe someone has better terms or definitions?
- afarrell 7y agoIn natural language, an idiom is a metaphor that has kicked the bucket. (Back when more people were involved in agriculture, they knew that a “bucket” was a type of frame used in slaughtering livestock.)
- riazrizvi 7y agoThey are idiomatic to people who haven’t learned how to program. Perhaps it was named with Product Managers, Project Managers, QA, and Business Analysts in mind?
- geoelectric 7y agoI think you captured definitions really well, but I'm not sure you captured the spirit of a programming idiom. The spirit is essentially pattern-like, that the specific syntax has a higher meaning in that language such that as soon as you see its shape you know what's up. Moreover, if you don't see that shape it doesn't register as "native" for that language. In your explanation, the spirit is "entry point is main." It's true the mechanics are unusual to Python, but that's not necessary and not all that counts. If I did the name/main comparison some other way it wouldn't be idiomatic because that very specific construct is the idiom. Some examples in other languages that aren't that hard to read mechanically: param = param || default; // idiomatic for ES5 default let bool = !!(whatever) // idiomatic for boolean coercion in JS reversed_list = forward_list[::-1] # idiomatic for sequence reversal in Python x = x >> 1 // idiomatic for x/2 in C In some of those cases there are practical realities that also drive them (the bitshift divide in particular comes from embedded and other speed concerns) but they aren't constructs that you see elsewhere very much outside that language even if they're valid. That's what makes them idiomatic to that language.
- saagarjha 7y ago> the bitshift divide in particular comes from embedded and other speed concerns Please don't do this unless you are using a really bad toolchain: most compilers will optimize x /= 2 for you.
- voltagex_ 7y agohttps://godbolt.org/z/nwk0Jm https://godbolt.org/z/nwk0Jm and https://godbolt.org/z/bGcD4Y https://godbolt.org/z/bGcD4Y show some differences - someone much better at assembly will have to explain this one.
- klipt 7y agohttps://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/ https://www.geeksforgeeks.org/left-shift-right-shift-operato... "The left shift and right shift operators should not be used for negative numbers. The result of is undefined behaviour if any of the operands is a negative number." Might be one reason?
- deleted 7y ago[deleted]
- throwaway66920 7y agoMy favorite quote from Archer: Noah: "Do you even know what an idiom is?" Archer: "colloquial metaphor" The phrase “colloquial metaphor” itself feels idiomatic from a context you’ve never considered, but appears to be obviously correct.