Skip to content

Commit

Permalink
PR_12_addTipOfPicAboutScope
Browse files Browse the repository at this point in the history
add a tip about Scope
  • Loading branch information
sxuya authored and xiaolai committed May 8, 2019
1 parent 5ddd49c commit d7304b2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Part.3.B.2.classes-2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@
"\n",
"如果你足够细心,你会发现这个版本中,有些变量前面有两个下划线 `__`,比如,`__life_span` 和 `self.__active`。这是 Python 的定义,变量名前面加上一个以上下划线(Underscore)`_` 的话,那么该变量是 “私有变量”(Private Variables),不能被外部引用。而按照 Python 的惯例,我们会使用两个下划线起始,去命名私有变量,如:`__life_span`。你可以回去试试,把所有的 `__life_span` 改成 `_life_span`(即,变量名开头只有一个 `_`,那么,`hasattr(Golem, '_life_span')` 和 `hasattr(g, '_life_span')` 的返回值就都变成了 `True`。\n",
"\n",
"看看下面的图示,理解起来更为直观一些:\n",
"看看下面的图示,理解起来更为直观一些,其中每个方框代表一个 Scope\n",
"\n",
"![](images/class-variables-scope.png)\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion markdown/Part.3.B.2.classes-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ UnboundLocalError: local variable 'population' referenced before assignment

如果你足够细心,你会发现这个版本中,有些变量前面有两个下划线 `__`,比如,`__life_span``self.__active`。这是 Python 的定义,变量名前面加上一个以上下划线(Underscore)`_` 的话,那么该变量是 “私有变量”(Private Variables),不能被外部引用。而按照 Python 的惯例,我们会使用两个下划线起始,去命名私有变量,如:`__life_span`。你可以回去试试,把所有的 `__life_span` 改成 `_life_span`(即,变量名开头只有一个 `_`,那么,`hasattr(Golem, '_life_span')``hasattr(g, '_life_span')` 的返回值就都变成了 `True`

看看下面的图示,理解起来更为直观一些:
看看下面的图示,理解起来更为直观一些,其中每个方框代表一个 Scope

![](https://raw.githubusercontent.com/selfteaching/the-craft-of-selfteaching/master/images/class-variables-scope.png?raw=true)

Expand Down

0 comments on commit d7304b2

Please sign in to comment.