我的編輯器(TextMate)以其他顏色(當用作變量名稱)顯示id
,然後顯示我通常的變量名稱。它是一個關鍵字嗎?我不想遮陽任何關鍵字...在idthon中`id`是關鍵字嗎?
29
A
回答
47
id
不是一個Python 關鍵字,但它是一個built-in function的名稱。
關鍵字are:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
關鍵字是無效的變量名。下面是一個語法錯誤:
if = 1
在另一方面,內置功能,如id
或type
或str
可以陰影:
str = "hello" # don't do this
7
它是一個內置的功能:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
13
您還可以從python獲得幫助:
>>> help(id)
Help on built-in function id in module __builtin__:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
或者你也可以質疑的IPython
IPython 0.10.2 [on Py 2.6.6]
[C:/]|1> id??
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function id>
Namespace: Python builtin
Docstring [source file open failed]:
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
6
檢查,如果事情是一個Python關鍵字:
>>> import keyword
>>> keyword.iskeyword('id')
False
檢查所有關鍵字的Python:
>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield']
相關問題
- 1. 在Python中是'輸入'關鍵字嗎?
- 2. 是'(A)'的關鍵字嗎?
- 3. @interface是Java中的關鍵字嗎?
- 4. 'file'是python中的關鍵字嗎?
- 5. 是javascript中的關鍵字不好嗎?
- 6. 是$ _SESSION ['user_id']中的'user_id'關鍵字嗎?
- 7. Perforce是否有$ Id $關鍵字?
- 8. '是'VB.NET關鍵字與Object.ReferenceEquals相同嗎?
- 9. 'value'是一個java關鍵字嗎?
- 10. 是Hive中的「關注」保留關鍵字嗎?
- 11. 在MySQL全文搜索中,'第二'是關鍵字嗎?
- 12. 字是節點js中的關鍵字嗎?
- 13. 是Python中的關鍵字
- 14. 在javascript中是否有關鍵字'capital'?
- 15. 是「日期」在mysql中的關鍵字?
- 16. 是'settings'在jQuery中的關鍵字?
- 17. scipy.stats.gaussian_kde中的關鍵字有誤嗎?
- 18. Perl中有關鍵字'with'嗎?
- 19. Python中的'is'關鍵字與JS中的===關鍵字相同嗎?
- 20. 如何在Python 2.7中將關鍵字從關鍵字複製到關鍵字?
- 21. 「標籤」是git中的保留關鍵字嗎?
- 22. 'media'是Android瀏覽器JavaScript中的保留關鍵字嗎?
- 23. 「Del」是Powershell中的保留關鍵字嗎?
- 24. rails:是「pollresponse」是查詢字符串中不允許的魔術關鍵字嗎?
- 25. Python變量分配和`id`關鍵字
- 26. 我應該在getter/setter中使用private關鍵字還是public關鍵字?
- 27. 在關鍵字使用的關鍵字中在查詢
- 28. 在關鍵字
- 29. 如何在SuggestBOX中保存id以外的關鍵字?
- 30. 修復搜索表單來拉關鍵字,而不是id值
感謝您的快速回復。我假設在'class'中使用'id'作爲屬性是一樣的嗎? 'myobject = myclass(); myobject.id = 123;'這是否會影響內置功能? – Aufwind 2011-06-14 22:27:27
@Aufwind:使用'id'作爲類屬性並不是很糟糕,因爲在Python中你總是要用某些東西('this.id'或'foo.id')來限定它,所以它總是遵循'.' 。你的編輯可能不理解這個區別。 – 2011-06-14 22:30:55
隨意編輯和添加關鍵字。 – Trufa 2011-06-14 22:31:39