Python 缺少 1 个必需的位置参数:'self'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20815887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:16:47  来源:igfitidea点击:

missing 1 required positional argument:'self'

python

提问by micma

here is my code:

这是我的代码:

class Email_Stuff():
    def __init__(self):
        self.emailaddr = None
        self.recipaddr = None
        self.EmailUser = None
        self.EmailPass = None
    def From_Email(self):
        self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    def To_Email(self):
        self.recipaddr = turtle.textinput("Client Email", "What is your client's email address?")
    def Email_Username(self):
        self.EmailUser = turtle.textinput("Your Email Username", "What is your email username?")
    def Email_Password(self):
        self.EmailPass = turtle.textinput("Your Email Password", "What is your email Password?")
    def Send_Email(self):
        print (self.emailaddr) #these are here for me to see if it is the right input
        print(self.recipaddr)
        print(self.EmailUser)
        print(self.EmailPass)
        import smtplib
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.login((self.EmailUser),(self.EmailPass))
        self.message = "Python Test Email"
        server.sendmail(self.emailaddr,self.recipaddr,self.message)

I have a button connected to Email_Stuff.From_Email and a button connected to Email_Stuff.From_Email etc...

我有一个连接到 Email_Stuff.From_Email 的按钮和一个连接到 Email_Stuff.From_Email 等的按钮...

Whenever I press the button to open up the turtle window it gives me this error:

每当我按下按钮打开乌龟窗口时,它都会给我这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
Fileline "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", 1475, in __call__
return self.func(*args)
TypeError: From_Email() missing 1 required positional argument: 'self'

But then if I take out the selfs from the From_Email and To_Email etc..

但是如果我从 From_Email 和 To_Email 等中取出 selfs..

class Email_Stuff():
    def __init__(self):
        self.emailaddr = None
        self.recipaddr = None
        self.EmailUser = None
        self.EmailPass = None
    def From_Email():
        self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    def To_Email():
        self.recipaddr = turtle.textinput("Client Email", "What is your client's email address?")
    def Email_Username():
        self.EmailUser = turtle.textinput("Your Email Username", "What is your email username?")
    def Email_Password():
        self.EmailPass = turtle.textinput("Your Email Password", "What is your email Password?")
    def Send_Email(self):
        print (self.emailaddr) #these are here for me to see if it is the right input
        print(self.recipaddr)
        print(self.EmailUser)
        print(self.EmailPass)
        import smtplib
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.login((self.EmailUser),(self.EmailPass))
        self.message = "Python Test Email"
        server.sendmail(self.emailaddr,self.recipaddr,self.message)

I get this error message (this isnt all of it):

我收到此错误消息(这不是全部):

    self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    NameError: global name 'self' is not defined

here is the button code:

这是按钮代码:

Email_Button = Button(root, text='Enter Your Email', command=Email_Stuff.From_Email)
Email_Button.pack()
Email_Button.place(x=250,y=210)

Please help! I'm sorry for the long post

请帮忙!我很抱歉这篇长文章

采纳答案by m01

I think you're hitting the following problem. If you take the following class F:

我认为您遇到了以下问题。如果您参加以下课程F

class F():
     def foo(self):
         return 1

and try to call F.foo(), you should get an error similar to the one you're seeing.

并尝试调用F.foo(),您应该会收到与您所看到的类似的错误。

>>> F.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with F instance as first argument (got nothing instead)

What you need to do, is call foo()on an object of F:

你需要做的是调用foo()一个对象F

>>> f=F()
>>> f.foo()
1

I have a button connected to Email_Stuff.From_Email and a button connected to Email_Stuff.From_Email etc...

我有一个连接到 Email_Stuff.From_Email 的按钮和一个连接到 Email_Stuff.From_Email 等的按钮...

You'll probably need to instantiate an object of Email_Stuff, and then call yourobject.From_Email(). (If your class Email_Stuffalso contains the GUI button handler stuff, you can just call self.From_Email()from the button handler)

您可能需要实例化 的对象Email_Stuff,然后调用yourobject.From_Email()。(如果您的类Email_Stuff还包含 GUI 按钮处理程序的内容,则可以self.From_Email()从按钮处理程序调用)

回答by nobe4

Did you create an instance of the Email_Stuff before calling method ? Because the self is the current object calling the method so it's needed.

您是否在调用方法之前创建了 Email_Stuff 的实例?因为 self 是调用该方法的当前对象,所以需要它。

class A():
    def __init__(self, attr):
        self.attr = attr
    def func(self):
        print(self.attr)

a = A(42)
a.func() # print 42
# or
A(32).func() # print 32

回答by Antonio Ramírez

Also You can avoid to write "self" in the function, as example:

你也可以避免在函数中写“self”,例如:

>>> class F():
        def foo():
            return 1

>>> F.foo()

..: 1