python 2.6异常.类型错误:未绑定方法_init_U()ESMTP客户端实例

2024-09-30 18:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和BuildBot的全新用户。当前我正在使用电子邮件警报 当BuildBot生成状态发生更改(从“成功”更改为“失败”,或反之亦然),并且每次生成失败时都会发送电子邮件。我在尝试发送电子邮件时遇到以下Python错误。在

--- <exception caught here> ---
**ESMTPClient.__init__(self, secret, contextFactory, *args, **kw)
exceptions.TypeError?: unbound method init() must be called with ESMTPClient 
instance as first argument (got ESMTPSender instance instead)**

我在网上找到了一些这个错误的例子,包括

You just need to pass 'self' as an argument to 'Thread.init' and calling the super class

但我仍然不确定为什么会出错。如果您能提供任何指导/帮助,说明为什么会出现此错误,以及如何着手解决该问题,我将不胜感激。我不是这段代码的作者,所以我不确定要寻找什么来解决这个问题。在

在以下代码从gmail帐户更改为公司帐户之前,该电子邮件正在工作。在

^{pr2}$

以下是生成异常的代码块:

class ESMTPSender(SenderMixin, ESMTPClient): 
    requireAuthentication = True 
    requireTransportSecurity = True 
    def __init__(self, username, secret, contextFactory=None, *args, **kw):  
        self.heloFallback = 0 
        self.username = username 

        if contextFactory is None: 
             contextFactory = self._getContextFactory() 

        ESMTPClient.__init__(self, secret, contextFactory, *args, **kw) 
        self._registerAuthenticators() 

SSA公司


Tags: instance代码selfsecretinit电子邮件as错误
1条回答
网友
1楼 · 发布于 2024-09-30 18:22:11

这似乎是一个很难出现的异常,通常除非从其他类继承,否则通常不会显式调用__init__。有一种情况下,您可能会得到这个错误:

class Foo(object):
    def __init__(self,*args):
       print("In Foo, args:",args,type(self))

class Bar(object):
    def __init__(self,*args):
        Foo.__init__(self,*args)  #Doesn't work.  Complains that the object isn't the right type.

要解决此问题,我们可以使BarFoo继承:

^{pr2}$

如果将BarFoo子类化是没有意义的,那么可以将公共代码分解成一个单独的函数:

def common_code(instance,*args):
    print("Common code: args",args,type(instance))

class Foo(object):
    def __init__(self,*args):
        common_code(self,*args)

class Bar(object):
    def __init__(self,*args):
        common_code(self,*args)

虽然这种问题很难诊断,如果没有实际看到产生错误的代码。在

相关问题 更多 >