I have a class that I pass various values (IP address, name, model ect) when I define the class.
At the same time I am putting these classes into a list so I can easily expand the number of devices. I want to be able to call a separate function from the class.
For example
My class looks somewhat like
class foo:
def __init__(self, IP, Name, Model, Series):
#Do stuff
def getstate():
#Do more stuff and return a value
My list looks somewhat like
Devices = {} #I fill this up with the list of all devices elseware
ClassDevices = []
for key in Devices:
print 'List is empty, store objects'
ClassDevices.append(Foo(Devices[key]["IP"], Devices[key], Devices[key]["Model"], Devices[key]["Series"]))
The above seems to work but now I want to be able to get the status of each device. I have tried the following
print ClassDevices[0].getstate()
And I get an error
TypeError: object cannot be interpreted as an index
I'm new to Python so I am not even sure if what I am trying to do is possible
Full code (since it probably is easier) can be seen here
What you are trying to do is pretty simple. The main issue is that you are not iterating correctly (because you are new to Python).
You have a piece of code that looks like this:
for x in SmartHome.SenseMeDevices:
print(SmartHome.SenseMeDevices[x].getstate())
In this case x
is already an element of your list, not an index to an element.
You should instead iterate like this:
for x in SmartHome.SenseMeDevices:
print(x.getstate())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.