1

If I look in python code and sometimes maybe also in C codes, there is often two variables with the same name except for the underscore. For example two variables

variable1 data;
variable2 _data;

Why is it like that and what is the background?

FrustratedWithFormsDesigner
  • 46,105
  • 7
  • 126
  • 176
Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95
  • 6
    For python it's a convention to mark fields that should be considered an implementation detail. See [In Python, what is the underscore in front of the instance variable?](https://stackoverflow.com/a/6700879/445517) and [PEP 8](https://www.python.org/dev/peps/pep-0008/#id36). – CodesInChaos May 05 '17 at 17:06
  • 1
    Possible duplicate of [Why aren't there explicit access modifiers in Python:](https://softwareengineering.stackexchange.com/questions/91799/why-arent-there-explicit-access-modifiers-in-python) – gnat May 05 '17 at 17:07
  • 1
    see also: [What is the historical reason why Python uses the double underscore for Class Private members](https://softwareengineering.stackexchange.com/a/228340/31260) – gnat May 05 '17 at 17:09
  • 5
    @gnat: That dupe is a bit of a stretch. Your "see also" would be a better dupe, had it mentioned single underscores. The post CodesInChaos mentioned is almost certainly a better dupe, if it were hosted on this site. – Robert Harvey May 05 '17 at 17:16
  • Also, since the OP mentions C, this link should be useful: http://stackoverflow.com/questions/25090635/use-and-in-c-programs – FrustratedWithFormsDesigner May 05 '17 at 19:18
  • All the comments seem to be focusing on the use of an underscore in a variable name for a specific language, when the actual question seems to be more about why you would have two variables that are identical, except that one has a leading underscore. In other words, is there a special meaning to the underscore when the name is otherwise identical to another variable. – Bryan Oakley May 05 '17 at 19:28

3 Answers3

3

In general it is to avoid a name clash. You have one variable and you need another which is a different incarnation, possibly in a different domain, yet it calls for the same name.

Like, an application level variable and a system level variable. Or a public property and its internal variable.

Martin Maat
  • 18,218
  • 3
  • 30
  • 57
  • its usually when code has been quickly transposed from c, into java. Mostly the underscore should be replaced by "this.". Though sometimes people prefix types with symbols in untyped languages for convenience. It looks ugly in java. – Richard May 05 '17 at 23:23
  • @Richard: I don't think you can say it is usually when converting C into Java. I've seen it used in several languages, it's not just a C thing or a Java thing, and it's not just when converting from one to another. – Bryan Oakley May 06 '17 at 03:50
1

Generally speaking...

For the case of two variable names that only differ by one leading underscore, I think the answer boils down to "the programmer thought it made the code more readable". Whether it actually makes the code more readable may be debatable, but sometimes it can make sense for tightly coupled variables.

For example, you may have a function that takes an argument that you apply some small transformation to before proceeding. Except for the transformation, the variables will contain identical information.

For example:

def some_function(foobar):
    _foobar = foobar.lower()
    if _foobar == "whatever":
        ...

If I had to give you a rule of thumb for how to interpret it, I would say it's probably safe to assume the variable with an underscore contains data that is only slightly different from the 'real' value.

If your question is specifically about why someone would use a leading underscore in python, PEP8 contains the official naming standards. Included in those naming standards are rules for leading and trailing underscores.

Bryan Oakley
  • 25,192
  • 5
  • 64
  • 89
  • 4
    That may be true "generally speaking", but the question is tagged with Python, and in Python, it has a [pretty specific meaning](http://stackoverflow.com/questions/6700826/in-python-what-is-the-underscore-in-front-of-the-instance-variable/6700879#6700879). – Vincent Savard May 05 '17 at 19:13
  • @VincentSavard: OP also mentions C. I'm not even sure if the meaning is exactly the same in both languages. – FrustratedWithFormsDesigner May 05 '17 at 19:19
  • @VincentSavard: right, it's tagged with python, but the actual question isn't "why do python programmers use leading underscores" but rather "why will a developer use two variables that only differ by a leading underscore", and then the OP mentions both Python and C. So, I took it to be more of a generic question than one tied specifically to python. Hence the leading _"Generally speaking..."_. – Bryan Oakley May 05 '17 at 19:19
  • Fair enough, however I do think an answer lacking a reference to PEP8 is incomplete in this context. The reason isn't "a developer thought it made the code more readable" if he followed established conventions. – Vincent Savard May 05 '17 at 19:21
  • @VincentSavard: I guess I'll have to respectfully disagree. Again, the question isn't why a developer should put a underscore before a variable, the question is why have two variables, one with an underscore and one without. PEP8 doesn't address that. I think the distinction is important. Regardless, I'll update my answer if you think that's important. – Bryan Oakley May 05 '17 at 19:23
  • I see what you mean. You're right, I didn't interpret the question like that at first. – Vincent Savard May 05 '17 at 19:35
0

I can't speak to Python.

In the case of C, identifiers with leading underscores are supposed to be reserved for use by the implementation:

7.1.3 Reserved identifiers

1     Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

      — All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

      — All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

      — Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).

      — All identifiers with external linkage in any of the following subclauses (including the future library directions) and errno are always reserved for use as identifiers with external linkage.184)

      — Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

2     No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

C 2011 Online Draft (N1570)

IOW, if you write any C code, you should not use leading underscores for your own identifiers (variable names, function names, and struct, union, and enum tag names); if you do, you run a slight risk of a name collision with an identifier used by the implementation during translation. For example, if you define a function named _write, this may conflict with a predefined function _write that's used by the printf implementation in the standard library.

Since the behavior on declaring or using a reserved identifier is undefined, the compiler is not obligated to issue any diagnostics; you won't know that there's a problem (if there is one) until runtime, and that problem may be fairly subtle and hard to diagnose. The compilers I'm familiar with won't issue any diagnostics or fail to translate the code.

Note that there's nothing magic about the underscore itself - it's just another character that you can use in an identifier. The only real difference between _data and data is that they are distinct names; the underscore doesn't add any meaning beyond this. Again, the intent is that names with leading underscores are reserved for use by the implementation, but the compiler typically doesn't enforce this during translation.

John Bode
  • 10,826
  • 1
  • 31
  • 43