Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0783eb

Browse files
committed
Translate faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
1 parente167727 commitd0783eb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

‎faq/design.po‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ msgstr ""
207207

208208
#:../../faq/design.rst:112
209209
msgid"Why must 'self' be used explicitly in method definitions and calls?"
210-
msgstr""
210+
msgstr"為何「self」在方法 (method) 定義和呼叫時一定要明確使用?"
211211

212212
#:../../faq/design.rst:114
213213
msgid""
214214
"The idea was borrowed from Modula-3. It turns out to be very useful, for a "
215215
"variety of reasons."
216-
msgstr"此構想從Modula-3而來。有許多原因可以說是非常實用。"
216+
msgstr"此構想從 Modula-3 而來。因為許多原因,他可以說是非常實用。"
217217

218218
#:../../faq/design.rst:117
219219
msgid""
@@ -227,6 +227,12 @@ msgid ""
227227
"Some C++ and Java coding standards call for instance attributes to have an "
228228
"``m_`` prefix, so this explicitness is still useful in those languages, too."
229229
msgstr""
230+
"第一,這樣可以更明顯表現出你在用方法 (method) 或是實例 (instance) 的屬性,而"
231+
"非一個區域變數。即使不知道類別 (class) 的定義,當看到 ``self.x`` 或 ``self."
232+
"meth()``,就會很清楚地知道是正在使用實例的變數或是方法。在 C++ 裡,你可以藉由"
233+
"沒有區域變數宣告來判斷這件事 ── 但在 Python 裡沒有區域變數宣告,所以你必須去"
234+
"看類別的定義來確定。有些 C++ 和 Java 的程式碼規格要求要在實例屬性的名稱加上前"
235+
"綴 ``m_``,所以這種明確性在那些語言也是很好用的。"
230236

231237
#:../../faq/design.rst:127
232238
msgid""
@@ -239,6 +245,11 @@ msgid ""
239245
"class method wants to extend the base class method of the same name and thus "
240246
"has to call the base class method somehow."
241247
msgstr""
248+
"第二,當你想明確地使用或呼叫在某個類別裡的方法的時候,你不需要特殊的語法。在 "
249+
"C++ 裡,如果你想用一個在繼承類別時被覆寫的基底類別方法,必須要用 ``::`` 運算"
250+
"子 -- 但在 Python 裡,你可以直接寫成 ``baseclass.methodname(self, <argument "
251+
"list>)``。這在 :meth:`__init__` 方法很好用,特別是在一個繼承的類別要擴充基底"
252+
"類別的方法而要呼叫他時。"
242253

243254
#:../../faq/design.rst:136
244255
msgid""
@@ -257,6 +268,14 @@ msgid ""
257268
"instance variables live in two different namespaces, and you need to tell "
258269
"Python which namespace to use."
259270
msgstr""
271+
"最後,他解決了關於實例變數指派的語法問題:因為區域變數在 Python 是(定義為)"
272+
"在函數內被指派值的變數(且沒有被明確宣告成全域),所以會需要一個方法來告訴直"
273+
"譯器這個指派運算是針對實例變數,而非針對區域變數,這在語法層面處理較好(為了"
274+
"效率)。C++ 用宣告解決了這件事,但 Python 沒有,而為了這個原因而引入變數宣告"
275+
"機制又略嫌浪費。但使用明確的 ``self.var`` 就可以把這個問題圓滿解決。同理,在"
276+
"用實例變數的時候必須寫成 ``self.var`` 即代表對於在方法中不特定的名稱不需要去"
277+
"看實例的內容。換句話說,區域變數和實例變數存在於兩個不同的命名空間 "
278+
"(namespace),而你需要告訴 Python 要使用哪一個。"
260279

261280
#:../../faq/design.rst:154
262281
msgid"Why can't I use an assignment in an expression?"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp