Questions tagged [idioms]

A programming idiom is the usual and customary way to write code in a particular language. Idioms are highly recognizable ways of overcoming a particular limitation and/or writing commonly-used code, sometimes with a purpose that is separate from the literal meaning of the code. An idiom can also be the standard way of implementing something when there are multiple ways to do it.

From Wikipedia:

A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task, algorithm, or data structure that is not a built-in feature in the programming language being used, or, conversely, the use of an unusual or notable feature that is built into a programming language.

Something is usually described as being of "idiomatic use" when the construct is common, brief and "comes naturally" to the user of the programming language.

1207 questions
8066
votes
46 answers

What does if __name__ == "__main__": do?

What does this do, and why should one include the if statement? if __name__ == "__main__": print("Hello, World!") If you are trying to close a question where someone should be using this idiom and isn't, consider closing as a duplicate of Why…
Devoted
  • 177,705
  • 43
  • 90
  • 110
1236
votes
43 answers

How can I initialise a static Map?

How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import…
dogbane
  • 266,786
  • 75
  • 396
  • 414
451
votes
17 answers

Get the key corresponding to the minimum value within a dictionary

If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function... Given the input: {320:1, 321:0, 322:3} It would return 321.
tjvr
  • 17,431
  • 6
  • 25
  • 26
430
votes
25 answers

Python idiom to return first item or None

I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: def main(): my_list = get_list() if len(my_list) > 0: …
Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
384
votes
11 answers

How to implement the factory method pattern in C++ correctly

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible…
Kos
  • 70,399
  • 25
  • 169
  • 233
308
votes
34 answers

What is the pythonic way to detect the last element in a 'for' loop?

How can I treat the last element of the input specially, when iterating with a for loop? In particular, if there is code that should only occur "between" elements (and not "after" the last one), how can I structure the code? Currently, I write code…
e.tadeu
  • 5,024
  • 2
  • 20
  • 21
297
votes
9 answers

How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map > For example, the above container holds data like this: m["name1"]["value1"] = "data1"; m["name1"]["value2"] =…
Jack
  • 3,769
  • 6
  • 24
  • 32
291
votes
47 answers

How do I reverse an int array in Java?

I am trying to reverse an int array in Java. This method does not reverse the array. for(int i = 0; i < validData.length; i++) { int temp = validData[i]; validData[i] = validData[validData.length - i - 1]; validData[validData.length -…
MichaelScott
  • 2,937
  • 2
  • 16
  • 4
267
votes
15 answers

When to use std::size_t?

I'm just wondering should I use std::size_t for loops and stuff instead of int? For instance: #include int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? …
nhaa123
  • 9,570
  • 11
  • 42
  • 63
221
votes
4 answers

Union of dict objects in Python

How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' :…
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
214
votes
17 answers

Python: most idiomatic way to convert None to empty string?

What is the most idiomatic way to do the following? def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) update: I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for…
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
193
votes
3 answers

When is it appropriate to use an associated type versus a generic type?

In this question, an issue arose that could be solved by changing an attempt at using a generic type parameter into an associated type. That prompted the question "Why is an associated type more appropriate here?", which made me want to know…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
166
votes
8 answers

What is the "Execute Around" idiom?

What is this "Execute Around" idiom (or similar) I've been hearing about? Why might I use it, and why might I not want to use it?
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
159
votes
6 answers

Check whether a variable is a string in Ruby

Is there anything more idiomatic than the following? foo.class == String
davidchambers
  • 23,918
  • 16
  • 76
  • 105
122
votes
10 answers

Pairs from single list

Often enough, I've found the need to process a list by pairs. I was wondering which would be the pythonic and efficient way to do it, and found this on Google: pairs = zip(t[::2], t[1::2]) I thought that was pythonic enough, but after a recent…
Apalala
  • 9,017
  • 3
  • 30
  • 48
1
2 3
80 81