我有类的简单的例子,在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