简体   繁体   English

将参数列表发送到 Python 类中的函数的最佳方法是什么?

[英]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.

相关问题 在Python类中检查必需参数的最佳方法 - Best Way to Check for Required Arguments in Python Class 将参数发送到Python脚本的规范方法是什么? - What is the canonical way to send arguments to a Python script? 将列表从 python function 导入 tkinter GUI 的最佳方法是什么 - What is the best way to Importing a list from a python function into tkinter GUI 在 Python 类构造函数中只允许一对参数中的一个的最佳方法是什么? - What is the best way to allow only one of a pair of arguments in a Python class constructor? 从脚本中发送python控制台输出作为电子邮件的最佳方法是什么? - What is the best way to send python console output as email from within the script? 在python中发送函数参数的参数 - send arguments for function arguments in python 装饰Python类方法的最佳方法是什么? - What is the best way to decorate methods of a Python class? 在从Python列表中提取数据的打印语句中运行循环的最佳方法是什么? - What would be the best way to run a loop within a print statement that is extracting data from a list in Python? 使用 argparse 将参数发送到 Python 脚本中的函数 - Use argparse to send arguments to function within Python script 在Python中保留排序列表的最佳方法是什么 - What is the best way to keep a sorted list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM