How to explain callbacks in plain english? How are they different from calling one function from another function?

I am going to try to keep this dead simple. A “callback” is any function that is called by another function which takes the first function as a parameter. A lot of the time, a “callback” is a function that is called when something happens. That something can be called an “event” in programmer-speak. Imagine … Read more

Simplest/cleanest way to implement a singleton in JavaScript

I think the easiest way is to declare a simple object literal: var myInstance = { method1: function () { // … }, method2: function () { // … } }; If you want private members on your singleton instance, you can do something like this: var myInstance = (function() { var privateVar=””; function privateMethod … Read more

How do Python functions handle the types of parameters that you pass in?

The other answers have done a good job at explaining duck typing and the simple answer by tzot: Python does not have variables, like other languages where variables have a type and a value; it has names pointing to objects, which know their type. However, one interesting thing has changed since 2010 (when the question … Read more

Run function from the command line

With the -c (command) argument (assuming your file is named foo.py): $ python -c ‘import foo; print foo.hello()’ Alternatively, if you don’t care about namespace pollution: $ python -c ‘from foo import *; print hello()’ And the middle ground: $ python -c ‘from foo import hello; print hello()’

Return value in a Bash function

Although Bash has a return statement, the only thing you can specify with it is the function’s own exit status (a value between 0 and 255, 0 meaning “success”). So return is not what you want. You might want to convert your return statement to an echo statement – that way your function output could … Read more

Why do some functions have underscores “__” before and after the function name?

From the Python PEP 8 — Style Guide for Python Code: Descriptive: Naming Styles The following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention): _single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose name starts with an underscore. single_trailing_underscore_: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)