Skip to content

Commit 85f4dc4

Browse files
duchengleisurelicht
ducheng
authored andcommitted
EX.Mangling time
1 parent 9437483 commit 85f4dc4

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ So, here we go...
7474
- [> Even Python understands that love is complicated/连Python也知道爱是难言的 *](#-even-python-understands-that-love-is-complicated连Python也知道爱是难言的-)
7575
- [> Yes, it exists!/是的, 它存在!](#-yes-it-exists是的-它存在)
7676
- [> Inpinity/无限 *](#-inpinity无限-)
77-
- [> Mangling time! *](#-mangling-time-)
77+
- [> Mangling time!修饰时间! *](#-mangling-time修饰时间-)
7878
- [Section: Miscellaneous](#section-miscellaneous)
7979
- [> `+=` is faster](#--is-faster)
8080
- [> Let's make a giant string!](#-lets-make-a-giant-string)
@@ -2138,7 +2138,7 @@ Try block executed successfully...
21382138
21392139
---
21402140
2141-
### > Mangling time! *
2141+
### > Mangling time!/修饰时间! *
21422142
21432143
```py
21442144
class Yo(object):
@@ -2157,13 +2157,13 @@ AttributeError: 'Yo' object has no attribute '__honey'
21572157
True
21582158
```
21592159
2160-
Why did `Yo()._Yo__honey` work? Only Indian readers would understand.
2160+
为什么 `Yo()._Yo__honey` 能运行? 只有印度人理解.(译: 这个梗可能是指印度音乐人[Yo Yo Honey Singh](https://en.wikipedia.org/wiki/Yo_Yo_Honey_Singh))
21612161
2162-
#### 💡 Explanation:
2162+
#### 💡 说明:
21632163
2164-
* [Name Mangling](https://en.wikipedia.org/wiki/Name_mangling) is used to avoid naming collisions between different namespaces.
2165-
* In Python, the interpreter modifies (mangles) the class member names starting with `__` (double underscore) and not ending with more than one trailing underscore by adding `_NameOfTheClass` in front.
2166-
* So, to access `__honey` attribute, we are required to append `_Yo` to the front which would prevent conflicts with the same name attribute defined in any other class.
2164+
* [名字修饰](https://en.wikipedia.org/wiki/Name_mangling) 用于避免不同命名空间之间名称冲突.
2165+
* Python 中, 解释器会通过给类中以 `__` (双下划线)开头且结尾最多只有一个下划线的类成员名称加上`_NameOfTheClass` 来修饰(mangles)名称.
2166+
* 所以, 要访问 `__honey` 对象,我们需要加上 `_Yo` 以防止与其他类中定义的相同名称的属性发生冲突.
21672167
21682168
---
21692169

0 commit comments

Comments
 (0)