【发布时间】:2016-07-09 09:52:07
【问题描述】:
我读到构造函数就像传递给类的第一个参数,这对我来说很有意义,因为参数似乎是通过__init__ 方法传递给类的。例如,
class NewsStory(object):
def __init__(self, guid, title, subject, summary, link):
self.guid = guid
self.title = title
self.subject = subject
self.summary = summary
self.link = link
def get_guid(self):
return self.guid
def get_title(self):
return self.title
def get_subject(self):
return self.subject
def get_summary(self):
return self.summary
def get_link(self):
return self.link
firstStory = NewsStory('Globally Unique Identifier', \
'first_title','first_subject','This is an example \
sumary','example_link@something.com')
print firstStory.get_guid() # prints Globally Unique Identifier
所以当我“调用”类时,我将__init__ 方法的参数传递给它?我是新手,我读到的所有东西都很难理解和困惑。谢谢!
编辑 1
我发现这个问题有助于解释一些事情,比如 new 和 init 之间的区别,抱歉,我不知道如何添加链接,得删掉并粘贴:What can `__init__` do that `__new__` cannot?
【问题讨论】:
-
你真的试过不使用它吗?
标签: python class oop constructor initialization