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?

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.

  • 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 5 '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 6 '17 at 3: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.

  • 4
    That may be true "generally speaking", but the question is tagged with Python, and in Python, it has a pretty specific meaning. – Vincent Savard May 5 '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 5 '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 5 '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 5 '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 5 '17 at 19:23
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.