0

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.

For example, I want the instances Probe1, Probe2, and Probe3 created with their corresponding arguments. 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. 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])
2
  • 2
    There are many problems here. First of all, after each iteration, you are overwriting your x variable, so you basically lose all previously instantiated objects of max31865. Second, you are trying to call readTemp, which is a method defined in your max31865 class, from an outer scope, which is not possible. I am not sure what you are trying to accomplish here Commented Jul 6, 2018 at 3:02
  • If you have an error message please include it in the question. It's a great way to give specific information about your error, that you may not have realised was helpful or relevant. Commented Jul 6, 2018 at 3:07

2 Answers 2

1

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:

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):

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Tomforge, thanks for the answer. This is what I was looking for. Much appreciated!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.