Most Popular

1500 questions
3352
votes
34 answers

"Least Astonishment" and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function called with no parameter to always return a list with only…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
3344
votes
12 answers

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop application. We have large amounts of configuration…
Mike Willekes
  • 5,960
  • 10
  • 33
  • 33
3343
votes
14 answers

How do I copy a folder from remote to local using scp?

How do I copy a folder from remote to local host using scp? I use ssh to log in to my server. Then, I would like to copy the remote folder foo to local /home/user/Desktop. How do I achieve this?
Slasengger
  • 33,707
  • 3
  • 14
  • 10
3339
votes
83 answers

How do I iterate over the words of a string?

How do I iterate over the words of a string composed of words separated by whitespace? Note that I'm not interested in C string functions or that kind of character manipulation/access. I prefer elegance over efficiency. My current solution: #include…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
3337
votes
31 answers

pretty-print JSON using JavaScript

How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.
Mark
  • 67,098
  • 47
  • 117
  • 162
3326
votes
42 answers

Why is "using namespace std;" considered bad practice?

I have heard using namespace std; is bad practice, and that I should use std::cout and std::cin directly instead. Why is this? Does it risk declaring variables that share the same name as something in the std namespace?
akbiggs
  • 34,001
  • 6
  • 25
  • 36
3315
votes
27 answers

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

What do *args and **kwargs mean in these function definitions? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass See What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? for the complementary question…
Todd
  • 34,500
  • 4
  • 23
  • 13
3310
votes
51 answers

How can I check if an object is an array?

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can loop over it without fear of an error. So how do I check if the…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
3294
votes
13 answers

How do you push a tag to a remote repository using Git?

I added a tag to the master branch on my machine: git tag mytag master How do I push this to the remote repository? Running git push gives the message: Everything up-to-date However, the remote repository does not contain my tag.
Jonas
  • 121,568
  • 97
  • 310
  • 388
3292
votes
34 answers

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList places = new ArrayList(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as…
Macarse
  • 91,829
  • 44
  • 175
  • 230
3284
votes
17 answers

How can I delete a file or folder in Python?

How can I delete a file or folder in Python?
Zygimantas
  • 33,165
  • 6
  • 21
  • 23
3259
votes
40 answers

How do I pass a variable by reference?

I wrote this class for testing: class PassByReference: def __init__(self): self.variable = 'Original' self.change(self.variable) print(self.variable) def change(self, var): var = 'Changed' When I tried…
David Sykes
  • 48,469
  • 17
  • 71
  • 80
3259
votes
34 answers

How do I make git use the editor of my choice for editing commit messages?

How do I globally configure git to use a particular editor (e.g. vim) for commit messages?
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
3256
votes
15 answers

Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?

Mod note: This question is about why XMLHttpRequest/fetch/etc. on the browser are subject to the Same Access Policy restrictions (you get errors mentioning CORB or CORS) while Postman is not. This question is not about how to fix a "No…
Mr Jedi
  • 33,658
  • 8
  • 30
  • 40
3252
votes
20 answers

How to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
Jake Stewart
  • 32,729
  • 3
  • 20
  • 17