Skip to content

Commit fe65a1a

Browse files
author
ducheng
committed
EX.Mangling time
1 parent 4edb207 commit fe65a1a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

README.md

+7-7
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)
@@ -2132,7 +2132,7 @@ Try block executed successfully...
21322132
21332133
---
21342134
2135-
### > Mangling time! *
2135+
### > Mangling time!/修饰时间! *
21362136
21372137
```py
21382138
class Yo(object):
@@ -2151,13 +2151,13 @@ AttributeError: 'Yo' object has no attribute '__honey'
21512151
True
21522152
```
21532153
2154-
Why did `Yo()._Yo__honey` work? Only Indian readers would understand.
2154+
为什么 `Yo()._Yo__honey` 能运行? 只有印度人理解.(译: 这又是什么梗?)
21552155
2156-
#### 💡 Explanation:
2156+
#### 💡 说明:
21572157
2158-
* [Name Mangling](https://en.wikipedia.org/wiki/Name_mangling) is used to avoid naming collisions between different namespaces.
2159-
* 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.
2160-
* 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.
2158+
* [名字修饰](https://en.wikipedia.org/wiki/Name_mangling) 用于避免不同命名空间之间名称冲突.
2159+
* Python 中, 解释器会通过给类中以 `__` (双下划线)开头且结尾最多只有一个下划线的类成员名称加上`_NameOfTheClass` 来修饰(mangles)名称.
2160+
* 所以, 要访问 `__honey` 对象,我们需要加上 `_Yo` 以防止与其他类中定义的相同名称的属性发生冲突.
21612161
21622162
---
21632163

0 commit comments

Comments
 (0)