newb syntax question

Depends entirely on the language, the programmer and the environment.

In python it's usually a hint that a variable or method is not supposed to be used outside the class it's in. Python doesn't have public/private, but it's an indication that you should treat something as private.
(Or is it a double underscore? Anyway.)

In other languages that has private or protected variables and methods it might still be used, as a sort of hint.

I have also seen it used when you need a name that's similar to but distinct from another. Having a constructor take an argument '_x' that's used to initialize the variable 'x ' would be an example.

And I'm sure there are several uses I haven't listed.
edit: I've never seen it used to indicate a global var, but I'm not suprised. :)
 
in the past, some languages used it to tell the compiler that the variable was a global variable ("usertalk" comes to mind).

nowadays, it seems that people just kept that syntax as a visual indicator to the reader/developer that the variable was global, kinda in the same regards as hungarian notation prefixes from my VB6 development days.

i've seen people use the prefixed underscore to indicate global variables, as well as putting the variable in all caps with underscores separating key words, such as "_myGlobalVariable" and "MY_GLOBAL_VARIABLE".

other posters may have more info from their knowledge/experience on this as well, but this should at least answer your question.
 
Looks like they're using it for the two reasons I listed.

As a hint that _printPrimitives is private, and as a way to separate the name of the constructor argument from the private variable.
Having it the other way around form what I said [1] makes sense when you combine it with the "prefix private stuff with underscore"-rule above.

[1] Here, the argument is printObject and the private variable _printObject.
 
Back
Top