You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: faq/underscore-convention.md
+11-10Lines changed: 11 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,14 +18,15 @@ instead of
18
18
def sigmoid(self, z):
19
19
return 1.0 / (1.0 + np.exp(-z))
20
20
21
-
The short answer is, the trailing underscore (`self.gamma_`) in class attributes is a scikit-learn convention to denote "estimated" or "fitted" attributes. The leading underscores are (`_sigmoid(self, z)`) denote private methods that the user shouldn't bother with.
21
+
The short answer is, the trailing underscore (`self.gamma_`) in class attributes is a scikit-learn convention to denote "estimated" or "fitted" attributes.
22
+
The leading underscores are (`_sigmoid(self, z)`) denote private methods that the user should not bother with.
22
23
23
24
24
25
In brief: As a reader, you can safely ignore those underscores, however, if you are curious about their intention, please read on!
25
26
26
27
## Leading underscores in class methods
27
28
28
-
The usage of underscores for naming class methods is a common Python convention to distinguish between private and public methods. Basically, you don't want the user to worry about these private methods, which is why they don't appear in the help menu.
29
+
The usage of underscores for naming class methods is a common Python convention to distinguish between private and public methods. Basically, you do not want the user to worry about these private methods, which is why they do not appear in the help menu.
29
30
30
31
class MyClass(object):
31
32
def __init__(self, param='some_value'):
@@ -67,7 +68,7 @@ The usage of underscores for naming class methods is a common Python convention
67
68
68
69
* Note that `__init__` is an exception, `__init__` is a special method that is required to initialize a class.
69
70
70
-
Let’s initialize a new object and call this “public” class:
71
+
Let us initialize a new object and call this "public" class:
71
72
72
73
>>> MyObj = MyClass()
73
74
>>> MyObj.public()
@@ -81,7 +82,7 @@ The single underscore in `_indicate_private` indicates privacy. Typically, priva
81
82
82
83
- Please keep in mind that calling private methods is at your own risk; the developers usually take no responsibilities for odd things that may happen if you call private methods as a user.
83
84
84
-
The indication of “privacy” is a bit stronger if we use 2 preceding underscores, for example, calling the `__pseudo_private` method directly like a regular method doesn’t work anymore:
85
+
The indication of "privacy" is a bit stronger if we use 2 preceding underscores, for example, calling the `__pseudo_private` method directly like a regular method does not work anymore:
@@ -91,7 +92,7 @@ The indication of “privacy” is a bit stronger if we use 2 preceding undersco
91
92
92
93
AttributeError: 'MyClass' object has no attribute '__pseudo_private'
93
94
94
-
To call the a private methods that is prefaced with 2 underscores, we need to adhere to the “name mangling” rules; that is, we need to add a `_classname` prefix, to call the method, for example,
95
+
To call the a private methods that is prefaced with 2 underscores, we need to adhere to the "name mangling" rules; that is, we need to add a `_classname` prefix, to call the method, for example,
95
96
96
97
>>> MyObj._MyClass__pseudo_private()
97
98
'really private method'
@@ -100,16 +101,16 @@ To call the a private methods that is prefaced with 2 underscores, we need to ad
100
101
101
102
## Class attributes with trailing underscores
102
103
103
-
In contrast to the leading underscore, the trailing underscores in class attributes don't any "technical" effects. In fact, this is just a convention that I adopted from scikit-learn out of habit.
104
+
In contrast to the leading underscore, the trailing underscores in class attributes do not have any "technical" effects. In fact, this is just a convention that I adopted from scikit-learn out of habit.
104
105
105
106
Here are two excerpts from the scikit-learn [developer/contributor documentation](http://scikit-learn.org/stable/developers/):
106
107
107
-
> Attributes that have been estimated from the data must always have a name ending with trailing underscore, for example, the coefficients of some regression estimator would be stored in a coef_ attribute after fit has been called.
108
+
> Attributes that have been estimated from the data must always have a name ending with trailing underscore`_`, for example, the coefficients of some regression estimator would be stored in a `coef_` attribute after `fit()` has been called.
108
109
109
110
110
-
> Also it is expected that parameters with trailing _ are not to be set inside the ``__init__`` method. All and only the public attributes set by fit have a trailing _. As a result the existence of parameters with trailing _ is used to check if the estimator has been fitted.
111
+
> Also it is expected that parameters with trailing underscore `_` are not to be set inside the ``__init__`` method. All and only the public attributes set by `fit()` have a trailing `_`. As a result the existence of parameters with trailing `_` is used to check if the estimator has been fitted.
111
112
112
-
To see it in action, let's create a primitive `Estimator`:
113
+
To see it in action, let us create a primitive `Estimator`:
113
114
114
115
class MyEstimator():
115
116
def __init__(self):
@@ -118,7 +119,7 @@ To see it in action, let's create a primitive `Estimator`:
118
119
def fit(self):
119
120
self.fit_param_ = 0.1
120
121
121
-
Intuitively, attributes that are in `__init__` are accessible after we initialized a new object
122
+
Intuitively, attributes that are in `__init__` are accessible after we initialized a new object:
0 commit comments