简体   繁体   English

如何在 Python 中创建同一个类的多个实例并传递不同的参数

[英]How to create multiple instances of a same class and pass different arguments in Python

I am trying to create a function to create 4 instances of a class Comparer.我正在尝试创建一个函数来创建类比较器的 4 个实例。 The class Comparer will compare two csv files with each other.类比较器将相互比较两个 csv 文件。 Thus, while creating the instances I need to pass different因此,在创建实例时,我需要传递不同的

I have a class like this:我有一个这样的课程:

class Comparer:
    def __init__(
        self
        , file1
        , file2
        , key_column):
    
    self.file1 = file1 
    self.file2 = file2
    self.key_column = key_column 

    # Read in the two files
    file1 = pd.read_csv(self.file1)
    file2 = pd.read_csv(self.file2)

    def compare():
        pass 
     

I would create a single instance like this and pass arguments我会像这样创建一个实例并传递参数

comparison1 = Comparer(
    file1 = "xyz/file1.csv",
    file2 = "xyz/file2.csv",
    key_col = 'col1')

comparison1.compare()    

But, I would prefer to create multiple instances in a loop or something which would be efficient.但是,我更喜欢在循环中创建多个实例或一些有效的方法。 I thought it could be done in the following way.我认为可以通过以下方式完成。

LIST = [comparison1, comparison2, comparison3, comparison4]

objects = [Comparer() for i in LIST]
for obj in objects:
    other_object.add(obj)   

But, I do not know how to change that to be able to pass different arguments (path to the correct .csv) for the different datasets needed in the comparison.但是,我不知道如何更改它以便能够为比较中所需的不同数据集传递不同的参数(正确 .csv 的路径)。

Has someone an idea?有人有想法吗?

You could try something like defining comparison_metadata:你可以尝试定义comparison_metadata之类的东西:

comparison_metadata= [
    {
        'file1':'xyz/file1a.csv',
        'file2':'xyz/file2a.csv',
        'key_column':'col1a'
    },
    {
        'file1':'xyz/file1b.csv',
        'file2':'xyz/file2b.csv',
        'key_column':'col1b'
    },
]

and then:进而:

comparers = [Comparer(metadata['file1'], metadata['file2'], metadata['key_column']) for metadata in comparison_metadata]

validation check:验证检查:

print(comparers[1].file1)
print(comparers[1].file2)
print(comparers[1].key_column)

xyz/file1b.csv
xyz/file2b.csv
col1b
class Comparer:
    def __init__(self, file1, file2, key_column):

        self.file1 = file1 
        self.file2 = file2
        self.key_column = key_column 

        # Read in the two files
        file1 = self.file1
        file2 = self.file2

    def compare(self):
        pass
     

comparer_data = {
    0: ["xyz/file1.csv", "xyz/file2.csv", "key1"],
    1: ["abc/file1.csv", "abc/file2.csv", "key2"]
}

comparers = [Comparer(file1= comparer_data[i][0], file2= comparer_data[i][1], key_column= comparer_data[i][2]) for i in comparer_data]
many_args = [
    ["a/file1.csv", "a/file2.csv", "key2"],
    ["b/file1.csv", "b/file2.csv", "key2"],
    ["c/file1.csv", "c/file2.csv", "key2"],
]

comparers = [Comparer(*args) for args in many_args]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python如何在迭代中创建同一个类的不同实例 - python how to create different instances of the same class into an iteration 如何创建多个 class 对象并在 Python 中使用循环传递 arguments? - How to create multiple class objects and pass arguments with loop in Python? 如何在 python 中创建装饰器 class 的不同实例 - How to create different instances of decorator class in python 如何在python中命名和创建一个类的多个实例? - how to name and create multiple instances of a class in python? 如何创建具有多个参数的类实例的列表? - How do I create a list of class instances that have multiple arguments? Python为同一类创建不同的变量名称和实例 - Python Create different variable names and instances for the same class 创建具有不同参数的类的多个对象 - create multiple objects of a class with different arguments 如何使用相同的参数创建相同的实例,但在python中使用不同的参数 - How to make same instance with same arguments but different for different arguments in python 如何异步运行同一个 class 的多个实例? - How to run multiple instances of the same class asynchrously? 如何从同一类的不同实例中访问类成员函数? 蟒蛇 - How to access class member functions from within different instances of that same class? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM