[英]What is the best way to send a list of arguments to a function within a class in Python?
I have a class called Dataset.我有一个名为 Dataset 的类。 Now within this dataset there is a function which has been defined to read a csv.
现在在这个数据集中有一个函数被定义为读取 csv。 Now some of those files comes with different encodings and delimiters etc., So I need to pass a filepath, encodings and delimiter to that function.
现在其中一些文件带有不同的编码和分隔符等,所以我需要将文件路径、编码和分隔符传递给该函数。 What is the best way to do this?
做这个的最好方式是什么? In future there may be need for few more of these arguments.
将来可能需要更多这样的论点。
class Dataset:
def __init__(self,path):
self.__fielpath=path
.......
def read(self):
data=pd.read_csv(self.__filepath)
For pd.read_csv()
need to send arguments what is the best way to do this?对于
pd.read_csv()
需要发送参数,最好的方法是什么?
Use **kwargs
to pass keyword arguments inside the function使用
**kwargs
在函数内部传递关键字参数
class Dataset:
def __init__(self,path):
self.__fielpath=path
.......
def read(self, **kwargs):
data=pd.read_csv(self.__filepath, **kwargs)
d = Dataset(path="some_path")
data = d.read(columns=["a","b","c"], skiprows=3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.