1

I want send argument to the function in the class but i got below error How Should i define a function can use the argument?

   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")

when I call the function in the class why i get below error?

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'
Chul Lee
  • 33
  • 4
  • You need to accept `self` on any class method. Otherwise, the first argument becomes `self`. – Charles Duffy Oct 30 '19 at 15:56
  • 1
    ...that said, the code in the question doesn't actually reproduce the problem, because its indentation is wrong. – Charles Duffy Oct 30 '19 at 15:56
  • I have add the self but still not work – Chul Lee Oct 30 '19 at 16:03
  • Well, yes -- just because you fix one problem doesn't mean you don't have more problems. We'd need to see those other problems before we can speak to them, however, and each Stack Overflow question should only be about *one* problem. – Charles Duffy Oct 30 '19 at 16:04

1 Answers1

1

Python automatically passes the instance of the class as the first argument to a member method. We normally call this argument self. So change your def to this:

    def writePost(self, m_title, m_content, m_category, m_tag):
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • File "/Users/chullee/github/tistory/class/tistory.py", line 111, in tistory.writePost("test","test","test","test") TypeError: writePost() takes 0 positional arguments but 5 were given – Chul Lee Oct 30 '19 at 15:58
  • def wrtiePost(self, m_title, m_content, m_category, m_tag): print("Start writePost") – Chul Lee Oct 30 '19 at 15:59
  • @ChulLee The code in your question won't give that error. Please provide a [mcve] that we call can run and get the same error you are asking about. – Code-Apprentice Oct 30 '19 at 16:01
  • @ChulLee First, check your spelling. `wrtiePost()` is not the same as `writePost()`. Second, check that your indentation in the code in your question is the same as what you have in your actual code. `class Tistory:` appears to be indented too far and I'm unsure what indentation you mean for `def writePost()`. – Code-Apprentice Oct 30 '19 at 16:02
  • @ChulLee If you still need help, please post a new question with your latest version of your code. – Code-Apprentice Oct 30 '19 at 16:06