在__init__中创建一个else函数

Abd*_*hab 1 python tornado

如何在if/else里面获取一个函数__init__:

class Foo(object):
    def __init__(self, q, **keywords):
        if a == "":
            print "No empty strings"
        else:
            def on_g(self, response):
                if response.error:
                    print "Check your internet settings"
                else:
                    self.Bar()
            http_client.fetch("http://www.google.com/", self.on_g)
Run Code Online (Sandbox Code Playgroud)

因为该程序不读取on_g()如果我把一个空字符串!

如果我使用on_g()外部并行,__init__()我需要一个声明的变量,例如:

class Foo(object):
    def __init__(self, q, **keywords):
        if a == "":
            print "No empty strings"
        else:
            self.on_g()
   def on_g(self):
       print 'hello there'
Run Code Online (Sandbox Code Playgroud)

将返回 hello there

Dav*_*ndy 5

你的错误在于

http_client.fetch("http://www.google.com/", self.on_g)
Run Code Online (Sandbox Code Playgroud)

应该是

http_client.fetch("http://www.google.com/", on_g)
Run Code Online (Sandbox Code Playgroud)

因为你定义了一个函数,而不是一个方法.