pythonreplace回调函数,python回调函数返回非 您所在的位置:网站首页 pythonreplace的用法 pythonreplace回调函数,python回调函数返回非

pythonreplace回调函数,python回调函数返回非

2023-09-01 12:06| 来源: 网络整理| 查看: 265

你说:I want the result to be 300 in the first instance and 700 in the

next instance, kind of generate a iterator object.

所以您只需将.provide方法转换为生成器,然后适当地调用它。像这样:def append_zeros(x,y):

x = int(str(x) + '00')

y = int(str(y) + '00')

#print x+y

return x + y

class Add_Nos():

def __init__(self,input_array):

self.input_array = input_array

def provide(self,callback):

for each in self.input_array:

x,y = each

yield callback(x,y)

add_nos = Add_Nos([(1,2),(3,4)])

for t in add_nos.provide(append_zeros):

print t

输出

^{pr2}$

那个append_zeros函数有点奇怪。与其将参数转换为字符串以便可以附加零,然后将结果转换回ints来进行算术,yu应该简单地将每个参数乘以100。在

另外,通过使用“splat”解包,您可以使.provide方法更加简化。正如tyteen4a03所提到的,在Python2中,Add_Nos类应该继承自object,这样你就得到了一个新样式的类,而不是弃用的旧样式类。所以这是另一个版本,它会产生与上面代码相同的输出。在def append_zeros(x, y):

return x * 100 + y * 100

class Add_Nos(object):

def __init__(self, input_array):

self.input_array = input_array

def provide(self, callback):

for each in self.input_array:

yield callback(*each)

add_nos = Add_Nos([(1,2),(3,4)])

for t in add_nos.provide(append_zeros):

print t



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有