我对 python 还是比较陌生,1-2 年的单独学习,并且正在尝试改进我的代码结构,所以我正在重构我编写的一些旧程序。在一个程序中,我定义了几种写入文件的方法。第一个使用“写”来转储一个巨大的 http 响应。第二个使用“writelines”转储各种派生列表,例如链接列表、表单或其他提取数据。
我最初考虑了文件的命名:
@property
def baseFilename(self):
unacceptable = re.compile(r'\W+')
fname = re.sub(unacceptable,'-',self.myUrl)
t = datetime.datetime.now()
dstring = "%s%s%s%s%s%s" % (t.year, t.month, t.day, t.hour, t.minute, t.second)
fullname = fname + '_' + dstring + '.html'
return fullname
但是我在每个写入方法中都有大量冗余代码块:
def writeFile(self, someHtml, writeMethod=write, prefix="RESPONSE_"):
'''The calling functions will supply only the data to be written and
static prefixes, e.g. "full_" for the entire http-response.
'''
fullpath = self.myDump + prefix + self.baseFilename
with open(fullpath, 'w') as h:
h.write(someHtml)
h.close()
print "saved %s" % fullpath
return fullpath
def writeList(self, someList, prefix="mechList_"):
'''Like write file but for one of the many lists outputted.
How do I refactor this, since redundant?
'''
fullpath = self.myDump + prefix + self.baseFilename
with open(fullpath, 'w') as h:
h.writelines(someList)
h.close()
print "saved %s" % fullpath
return fullpath
我希望能够为每个指定要使用的写入方法的函数添加一个变量,例如(writeMethod=writelines)。我考虑只传入一个字符串并使用其中一个黑魔法函数——我猜是 exec() ——但这不可能是正确的,因为似乎没有人使用过这些函数。这整个例子可能比较傻,因为我可以解决它,但我决定知道如何传递这些实例方法(这是正确的术语吗?)。这与绑定和解除绑定有关吗?我需要一个好的答案是传递'write','writelines'等所需的语法。可能很简单:writeMethod = insert_your_syntax_here。不过希望得到更多的解释或指导。谢谢。