To Underscore or to Not to Underscore, That Is the Question

To underscore or to not to underscore, that is the question

It will have no effect.

Part of the recommendations for writing CLS-compliant libraries is to NOT have two public/protected entities that differ only by case e.g you should NOT have

public void foo() {...}

and

public void Foo() {...}

what you're describing isn't a problem because the private item isn't available to the user of the library

The meaning of underscore in :not(_)

That's a simple Type selector, just one you're unlikely to find in normal circumstances.

var el = document.createElement('_');document.body.append(el);el.textContent = 'Hello';
_{ color: green; }
Say 

Underscore variable with walrus operator in Python

You are using the variable dummy, to filter the series. Therefore, don't replace it with _.

using underscore for a variable as same as keyword name in python

As stated in https://pep8.org/#function-and-method-arguments

If a function argument’s name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)

Note that a prefix underscore e.g. _class usually implies a private class attribute or method, something like:

class MyClass:
def __init__(self):
self._my_private_variable = 1

def _my_private_method(self):
pass

Some further info in https://pep8.org/#descriptive-naming-styles

_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_: used by convention to avoid conflicts
with Python keyword

What is the meaning of single and double underscore before an object name?

Single Underscore

In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way.
Using leading underscores for functions in a module indicates it should not be imported from somewhere else.

From the PEP-8 style guide:

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

Double Underscore (Name Mangling)

From the Python docs:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

And a warning from the same page:

Name mangling is intended to give classes an easy way to define “private” instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private.

Example

>>> class MyClass():
... def __init__(self):
... self.__superprivate = "Hello"
... self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

How do i replace letters with underscores but let the spaces stay in javascript

function randomWord() {
for (let i = 0; i < word.length; i++) {
answerArray[i] = word[i] == " " ? " " : "_"; // <==
console.log(answerArray[i]);
answer.innerHTML = answerArray.join(" ")
}
}

Explanation: Is the current character you're iterating a space in the word? If yes we insert a space (in this case an HTML space), if not we just insert an underscore.

Also, don't forget to change the assignment of the variable remainingLetters in your category() function so you're ignoring the space.

function category(type) {
/* ... */
remainingLetters = word.replace(/ /g, "").length;
/* ... */
}

Naming convention - underscore in C++ and C# variables

The underscore is simply a convention; nothing more. As such, its use is always somewhat different to each person. Here's how I understand them for the two languages in question:

In C++, an underscore usually indicates a private member variable.

In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

Before:

private string _name;
public string Name
{
get { return this._name; }
set { this._name = value; }
}

After:

public string Name { get; set; }

Understanding the declaration of the underscore in _.js?

var _ = function(obj) {
// Either serve as the identity function on `_` instances,
// ... or instantiate a new `_` object for other input.

// If an `_` instance was passed, return it.
if (obj instanceof _) return obj;
// If someone called `_(...)`, rather than `new _(...)`,
// ... return `new _(...)` to instantiate an instance.
if (!(this instanceof _)) return new _(obj);

// If we are instantiating a new `_` object with an underlying,
// ... object, set that object to the `_wrapped` property.
this._wrapped = obj;
};

// If there is an exports value (for modularity)...
if (typeof exports !== 'undefined') {
// If we're in Node.js with module.exports...
if (typeof module !== 'undefined' && module.exports) {
// Set the export to `_`
exports = module.exports = _;
}
// Export `_` as `_`
exports._ = _;
} else {
// Otherwise, set `_` on the global object, as set at the beginning
// ... via `(function(){ var root = this; /* ... */ }())`
root._ = _;
}


Related Topics



Leave a reply



Submit