我有類的簡單的例子,在python:面向對象的Python
class song:
def __init__(self, x):
print x
bang=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])
這工作。 但在另一本書詞「對象」創建一個新的類時使用:
class song(object):
def __init__(self,x):
print x
bang=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])
這工作了。另外,如果目的是通過,被取代的,例如,X:
class song(x):
def __init__(self,x):
print x
smile=song(['Our whole universe was in a hot dense state,Then nearly fourteen billion years ago expansion started, wait...'])
它不工作(NameError: name x is not defined
)。 什麼特別之處object
,據我所知,它甚至不是一個保留字,是不是?爲什麼代碼與它一起工作,而與x
- 不?
你可能想看看[Python中繼承(https://docs.python.org/2/tutorial/classes.html#inheritance),然後在[老VS新式類(HTTPS: //wiki.python。org/moin/NewClassVsClassicClass) – dhke
'NameError'通常是指嘗試引用不存在的內容。僅供將來參考。 – bcdan