【发布时间】:2020-02-25 23:35:10
【问题描述】:
我想向类中的函数发送参数,但出现以下错误 我应该如何定义一个可以使用参数的函数?
class Tistory:
def __init__(self, m_client_id, m_redirect_uri, m_user_id, m_password, m_blogname):
self.m_client_id = m_client_id
self.m_redirect_uri = m_redirect_uri
self.m_user_id = m_user_id
self.m_password = m_password
self.m_blogname = m_blogname
def wrtiePost(m_title, m_content, m_category, m_tag):
params = {
'blogName' : self.m_blogname,
'title' : m_title,
'content' : m_content,
'tag' : m_tag,
'category' : m_category,
'visibility' : '0',
#'published' : '',
#'slogan' : '',
#'acceptComment' : '1',
#'password' : '',
'access_token' : self.getAccessToken(),
'output' : 'json'
}
....
if __name__== "__main__":
tistory = Tistory(g_client_id,g_redirect_uri,g_user_id,g_password, g_blogname)
#tistory.getAccessToken()
tistory.writePost(m_title="test",m_content="test",m_category="test", m_tag="test")
当我在类中调用函数时为什么会出现以下错误?
Traceback (most recent call last):
File "/Users/chullee/github/tistory/class/tistory.py", line 113, in <module>
tistory.writePost(m_title="test",m_content="test",m_category="test", m_tag="test")
TypeError: writePost() got an unexpected keyword argument 'm_title'
【问题讨论】:
-
您需要在任何类方法上接受
self。否则,第一个参数变为self。 -
...也就是说,问题中的代码实际上并没有重现问题,因为它的缩进是错误的。
-
我已经添加了 self 但还是不行
-
嗯,是的——仅仅因为你解决了一个问题并不意味着你没有更多的问题。然而,在与他们交谈之前,我们需要先了解其他问题,而且每个 Stack Overflow 问题都应该只涉及一个问题。
标签: python