Is the underscore prefix for property and method names merely a convention?

Is the underscore prefix in JavaScript only a convention, like for example in Python private class methods are? From the 2.7 Python documentation: “Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with … Read more

Python 3: UnboundLocalError: local variable referenced before assignment [duplicate]

This question already has answers here: UnboundLocalError on local variable when reassigned after first use (12 answers) Closed 6 years ago. The following code gives the error UnboundLocalError: local variable ‘Var1’ referenced before assignment: Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print(“Result One”) elif Var2 == … Read more

What’s the scope of a variable initialized in an if statement?

I’m new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == ‘__main__’: x = 1 print x In other languages I’ve worked in, this code would throw an exception, as the x variable is local to the if statement … Read more

What underlies this JavaScript idiom: var self = this?

I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo: function Note() { var self = this; var note = document.createElement(‘div’); note.className=”note”; note.addEventListener(‘mousedown’, function(e) { return self.onMouseDown(e) }, false); note.addEventListener(‘click’, function() { return self.onNoteClick() }, false); this.note = note; // … } The author uses self in some places (the … Read more