Justice_Lords
Justice_Lords

Reputation: 969

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. Now within this dataset there is a function which has been defined to read a 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?

Upvotes: 1

Views: 55

Answers (1)

Sociopath
Sociopath

Reputation: 13401

Use **kwargs to pass keyword arguments inside the function

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)

Upvotes: 1

Related Questions