简体   繁体   English

使用列表创建对象

[英]Using a list of lists to create objects

I want to keep a list of temperature probes that will be taking temperature readings periodically. 我想保留一个将定期获取温度读数的温度探测器列表。 I want to store the arguments needed to create each instance of the temperature probe object in a list of lists. 我想将创建温度探测器对象的每个实例所需的参数存储在列表中。 I then want to create each instance from this list of lists and name each object using index 0 of each nested list. 然后,我想从该列表列表中创建每个实例,并使用每个嵌套列表的索引0命名每个对象。

For example, I want the instances Probe1, Probe2, and Probe3 created with their corresponding arguments. 例如,我希望使用它们的相应参数创建实例Probe1,Probe2和Probe3。 I then want to take a temperature reading from each probe in the list. 然后,我想从列表中的每个探针获取温度读数。

I want to be able to add unlimited probes without having to change the code down the line. 我希望能够添加无限的探针,而不必更改代码。

The issue I'm running into is when I try and do anything with Probe1, Probe2, or Probe3 python tells me they don't exist. 我遇到的问题是当我尝试使用Probe1,Probe2或Probe3 python做任何事情时,它们告诉我它们不存在。 I'm new to programming and I'm sure I'm missing something obvious. 我是编程新手,所以我肯定缺少明显的东西。

class max31865(object):
    def __init__(self, name, R_REF, csPin):
        self.name = name
        self.R_REF = R_REF
        self.csPin = csPin

    def readTemp(self):
        #code here to check temp


probe_list=[["Probe1", 430, 8],["Probe2", 430, 9],["Probe3", 430, 10]]

for probe in probe_list:
    x = str(probe[0])
    x = max31865(*probe)

for probe in probe_list:
    readTemp(probe[0])

I'm not sure what you want exactly but here are two likely usecases based on your question: 我不确定您到底想要什么,但是根据您的问题,这是两个可能的用例:

You want a simple list of probe objects, generated from a list of initialization arguments: 您需要一个由初始化参数列表生成的简单的探针对象列表:

The most straightforward way to do this is with the iterable unpacking operator ( * ) in combination with list comprehension : 最简单的方法是将可迭代的拆包运算符( * )与列表理解结合使用:

probe_list = [["Probe1", 430, 8],["Probe2", 430, 9],["Probe3", 430, 10]]
probe_obj_list = [max31865(*probe) for probe in probe_list]

Now you can call readTemp() on each object in the list, like so: 现在,您可以在列表中的每个对象上调用readTemp() ,如下所示:

probe_obj_list[1].readTemp() # Read the temperature of the second object

Or do it in a loop: 或循环执行:

for probe in probe_obj_list:
    probe.readTemp()

You want to be able to find probe objects by name: 您希望能够按名称查找探针对象:

Consider using a dictionary (also known as a map). 考虑使用字典 (也称为地图)。

probe_list = [["Probe1", 430, 8],["Probe2", 430, 9],["Probe3", 430, 10]]
probe_obj_map = {probe[0] : max31865(*probe) for probe in probe_list} # Dict comprehension

Now you can access the probe objects by name like so: 现在,您可以通过名称访问探针对象,如下所示:

probe_obj_map["Probe1"].readTemp() # Accessing the object mapped to by the string "Probe1"

And if you needed to loop through probe_list and find objects by name, you can (although i'm not sure why you would need to do that): 而且,如果您需要遍历probe_list并按名称查找对象,则可以(尽管我不确定为什么需要这样做):

for probe_args in probe_list:
    probe_obj_map[probe_args[0]].readTemp() # Access the object mapped to by the first argument of the nested list (i.e. the name)

Code corrections: 代码更正:

class Max31865(object):
    def __init__(self, name, R_REF, csPin): # missing `:` here
        self.name = name
        self.R_REF = R_REF
        self.csPin = csPin

    def read_temp(self):
        # code here to check temp
        # print the object's attributes or do anything you want
        print('Printing in the method: ', self.name, self.R_REF, self.csPin)


probe_list=[["Probe1", 430, 8],["Probe2", 430, 9],["Probe3", 430, 10]]

for probe in probe_list:
    # x = str(probe[0]) # probe[0] already is str
    x = Max31865(*probe) # Here x is instantiated as `Max31865` object
    print('Printing in the loop: ', x.name, x.R_REF, x.csPin)
    x.read_temp() # Then call the `read_temp()` method.

# for probe in probe_list:
#     readTemp(probe[0])
# This loop is confusing, just as @RafaelC noted in comment,
# 1. `readTemp` is a *method* of `Max31865` object, not a function you can call directly.
# 2. `readTemp` has no argument in it's definition, and you are giving one.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM