class A(obejct):
def all_answer(self):
#somecode
for s in br2n.split('\n'):
yield re.sub(re_allmark, '', s)
def save(self, path, pattern):
if not path:
raise 'No path'
else:
with open(path, pattern) as f:
f.write()
大致的效果
a = A()
for each in a.all_answer():
print each #正常返回结果
each.save('/Path/', 'pattern') #保存到文件中
如何让类中all_anser的值传入类中另一个函数呢?
1
horizon 2015-05-24 09:58:57 +08:00
self啊,你在all_answer中把想要传的值保存到self中,然后在save中使用就行了吧。
|
2
oott123 2015-05-24 10:04:33 +08:00 via Android
不应该是 a.save(each, path, pattern) 么…
|
3
cc7756789 OP answers = question.all_answer()
question.save(answers, '/home/zhg/Pictures/result.txt', 'w') 但是这种似乎不太人性化啊 期望的效果是 answers.save(....) |
4
dddd 2015-05-24 13:00:51 +08:00
for s in br2n.split('\n'): self.save(...)
OR: def save(path, mode): ----with open(path, mode) as f: --------for data in self.all_answer(): ------------self.write(data) OR: def save(path, mode): ----answers = self.all_answer() ----with open(path, mode) as f: --------while 1: ------------try: ----------------data = answers.next() ----------------f.write(data) ------------except StopIteration: ----------------break |
5
dddd 2015-05-24 13:15:28 +08:00
我懂你意思了。。。。
class A(obejct): ----def all_answer(self): --------for s in br2n.split('\n'): ------------yield B(re.sub(re_allmark, '', s)) class B(object): ----def __init__(self, data): --------self._data = data ----def save(self, path, pattern): ------------with open(path, pattern) as f: ----------------f.write(self._data) ---- def __str__(self): --------return self._data |