python - Python,如何在类方法中使用@
问题描述
我尝试@
在类方法中使用。像这样
class Dataset:
@parse_func
def get_next_batch(self):
return self.generator.__next__()
和这样的解析函数:
def parse_func(load_batch):
def wrapper(**para):
batch_files_path, batch_masks_path, batch_label = load_batch(**para)
batch_images = []
batch_masks = []
for (file_path, mask_path) in zip(batch_files_path, batch_masks_path):
image = cv2.imread(file_path)
mask = cv2.imread(mask_path)
batch_images.append(image)
batch_masks.append(mask)
return np.asarray(batch_images, np.float32), np.asarray(batch_masks, np.uint8), batch_label
return wrapper
但是,当我调用 时dataset.get_next_batch()
,它会引发 aexception
如下。
回溯(最近一次调用最后一次):TypeError:wrapper() 正好采用 0 个参数(给定 1 个)
您知道为什么会引发此错误以及任何解决方案吗?非常感谢!
解决方案
该函数wrapper(**kwargs)
只接受命名参数。但是,在实例方法中,self
自动作为第一个位置参数传递。由于您的方法不接受位置参数,因此它失败了。
您可以编辑为wrapper(self, **kwargs)
或更一般wrapper(*args, **kwargs)
的 . 但是,您使用它的方式,不清楚这些参数是什么。
推荐阅读
- excel - 如何将不同的excel数据表组合成一个表但不同的列?
- java - 如何在日期/时间范围内显示进度/搜索栏android
- excel - VBA 宏根据相邻值删除重复项及其相邻单元格
- stm32 - STM32L433RCTx USB MSC 读/写失败,GetCapacityFS 工作
- ag-grid - 获取添加的数据/行的节点ID
- excel - 在 CUBEVALUE() 中使用 OR()
- android - android 崩溃中的 Agroa 屏幕共享
- javascript - 防止在传递参数时立即调用函数
- php - 用于检查 Zip 文件有效性的 API 方法 - 之后 zip 没有关闭?
- reactjs - 警告:无法对未安装的组件执行 React 状态更新。反应