我想使用以下代码代理类的“all”方法:
import paramiko
class SFTPProxy():
def __init__(self, sftp):
self.sftp = TRANSPORT.open_sftp_client()
for x, y in type(self.sftp).__dict__.items():
if re.search(r'^__', x):
continue
def fn(self, *args, **kwargs):
return y(self.sftp, *args, **kwargs)
setattr(SFTPProxy, x, fn)
当我像这样调用方法时:
^{pr2}$它起作用了。在
当我像这样调用方法时:
fooproxy.listdir() # this is the method belongs to the proxied class
程序挂起了,代码中有什么浅显的问题吗?在
我在您的方法中看到的一个问题是,} 不是更简单更干净吗:
type(self.sftp).__dict__
中的值并非都是函数。因此,y(...)
将失败。重写^{这将相当迅速地处理所有类型的属性:实例/类字段、实例/类/静态方法。在
相关问题 更多 >
编程相关推荐