首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python2.6 exceptions.TypeError:未绑定方法_init_() ESMTP客户端实例

Python2.6 exceptions.TypeError:未绑定方法_init_() ESMTP客户端实例
EN

Stack Overflow用户
提问于 2012-07-16 13:37:08
回答 1查看 222关注 0票数 1

我是Python和BuildBot的全新用户。目前,当BuildBot构建状态更改时,我使用的是电子邮件警报(从成功移动到失败,反之亦然),每次生成失败时,失败都会发送电子邮件。尝试发送电子邮件时,我遇到下面的Python错误。

代码语言:javascript
复制
--- <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)**

我在网上搜索答案时发现了一些这个错误的例子,包括

您只需将'self‘作为参数传递给'Thread.init’,并调用超类

但我仍然不知道为什么会有错误。我希望能就这个错误发生的原因和如何解决这个问题提供任何指导/帮助。我不是这个代码的作者,所以我不知道要找什么来解决这个问题。

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

代码语言:javascript
复制
          c['status'].append(mail.MailNotifier(
                 fromaddr="load.builder@company.co.uk",
                 extraRecipients=["example@company.com",
                      ],
                 sendToInterestedUsers=False,
                 mode=('change', 'failing'),
                 relayhost="smtp.company.lan",
                 useTls=True,
                 smtpUser="lbuilder",
                 smtpPassword="password"))

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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2012-07-16 13:56:38

这似乎是一个困难的例外--通常,除非从其他类继承,否则不会显式调用__init__。这里有一种情况,您可以得到这个错误:

代码语言:javascript
复制
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继承

代码语言:javascript
复制
class Bar(Foo):
         #^ Bar now inherits from Foo
    def __init__(self,*args):
        Foo.__init__(self,*args) #This will work now since a Bar instance is a Foo instance

如果让BarFoo中子类没有意义,您可以将公共代码分解到一个单独的函数中:

代码语言:javascript
复制
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)

虽然这类问题很难诊断,但如果不实际看到产生错误的代码,就很难诊断。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11505558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档