如何在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
你的错误在于
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)
因为你定义了一个函数,而不是一个方法.
| 归档时间: |
|
| 查看次数: |
3396 次 |
| 最近记录: |