返回带有断开循环的字符串结果 [英] Return a string result with out breaking loop

查看:52
本文介绍了返回带有断开循环的字符串结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想知道是否还有XML RPC从服务器向客户端发送一串

文本而没有调用return因此打破了

我的循环


例如


def somefunc():

动作,结果中的文件:

full_filename = os.path.join(path_to_watch,files)

theact = ACTIONS.get(action," Unknown")

out2 = str(full_filename )+ " + str(theact)

返回out2


return语句将返回一个破坏循环的结果。我的目标是

让它继续循环并更新客户


任何想法?


干杯


Andrew


提前致谢


Pyhon 2.5.2


Windows XP

解决方案

Andrew写道:


你好我想知道如果无论如何都有XML RPC从服务器向客户端发送一串

文本而没有调用返回因此打破

我的循环


例如


def somefunc():

表示行动,结果中的文件:

full_filename = os .path.join(path_to_watch,files)

theact = ACTIONS.get(action," Unknown")

out2 = str(full_filename)+" " + str(theact)

返回out2


return语句将返回一个破坏循环的结果。我的目标是

让它继续循环并更新客户端


任何想法?



是的,使用yield而不是return。它将功能转变为发电机。


Christian


您好我一整天都在努力收益不同的东西:D


但我总是得到相同的结果使用收益


故障:<故障1:"< type' 'exceptions.TypeError''>:不能编组< type

''generator''


objects">



我不确定我做错了什么因为这是我第一次使用
产量


有关如何解决此错误的任何建议


干杯


Andrew

Christian Heimes写道:


Andrew写道:


>嗨我想知道是否还有XML RPC将一串
文本从服务器发送到客户端而没有调用返回因此
打破我的循环

def somefunc( ):
动作,结果中的文件:
full_filename = os.path.join(path_to_watch,files)
theact = ACTIONS.get(action,Unknown)
out2 = str(full_filename)+ " + str(theact)
返回out

返回语句将返回一个打破循环的结果。我的目标是让它继续循环并更新客户端

任何想法?



是的,使用yield而不是return。它将功能转变为发电机。


Christian


def somefunc():

表示行动,结果中的文件:

full_filename = os.path.join(path_to_watch,files)

theact = ACTIONS.get(action," Unknown" )

yield str(full_filename)+ " + str(theact)




这是一个例子,如果使用yield不起作用,以显示如何$ /
使用收益率:

def yi(x):

而x 0:

收益率str(x)

x - = 1


>> yi(4)



< 0x01FA3C88处的生成器对象>


>> a = yi(4)
a.next( )



''4''


>> a.next()



''3''


>> a.next()



''2''


>> a.next()



''1''


>> a.next()



Traceback(最近一次调用最后一次):

文件"< pyshell#151>",第1行,< module>

a.next()

StopIteration


>>>


Hi I was wondering if there is anyway with XML RPC to send a string of
text from the server to the client with out calling return thus breaking
my loop

for example

def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
out2 = str(full_filename) + " " + str(theact)
return out2

the return statement will return a result breaking my loop. My goal is
to have it continue looping and updating the client

any ideas?

Cheers

Andrew

Thanks in advance

Pyhon 2.5.2

Windows XP

解决方案

Andrew wrote:

Hi I was wondering if there is anyway with XML RPC to send a string of
text from the server to the client with out calling return thus breaking
my loop

for example

def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
out2 = str(full_filename) + " " + str(theact)
return out2

the return statement will return a result breaking my loop. My goal is
to have it continue looping and updating the client

any ideas?

Yes, use yield instead of return. It turns the function into a generator.

Christian


Hi Ive been trying with yield all day and other different things :D

but I always get the same result using yield

Fault: <Fault 1: "<type ''exceptions.TypeError''>:cannot marshal <type
''generator''

objects">

I''m not sure exactly what I am doing wrong as this is the first time Ive
used yield

Any suggestions on how to fix this error

Cheers

Andrew
Christian Heimes wrote:

Andrew wrote:

>Hi I was wondering if there is anyway with XML RPC to send a string of
text from the server to the client with out calling return thus
breaking my loop

for example

def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
out2 = str(full_filename) + " " + str(theact)
return out2

the return statement will return a result breaking my loop. My goal is
to have it continue looping and updating the client

any ideas?


Yes, use yield instead of return. It turns the function into a generator.

Christian


def somefunc():
for action, files in results:
full_filename = os.path.join(path_to_watch, files)
theact = ACTIONS.get(action, "Unknown")
yield str(full_filename) + " " + str(theact)

?
Here is an example if that doesn''t work, using yield, to show how to
use yield:
def yi(x):
while x 0:
yield str(x)
x -= 1

>>yi(4)

<generator object at 0x01FA3C88>

>>a=yi(4)
a.next()

''4''

>>a.next()

''3''

>>a.next()

''2''

>>a.next()

''1''

>>a.next()

Traceback (most recent call last):
File "<pyshell#151>", line 1, in <module>
a.next()
StopIteration

>>>


这篇关于返回带有断开循环的字符串结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆