I have a class set up that takes in a Dictionary (which contains a set of keys and values) and holds it in a local variable. There is a method that I call later that cycles through these keys, finds a matching one, and returns its associated value.
Later on in the program, I initialize instances of this object and add them to a list, and then later iterate through the list and request a specific property, but I'm getting a runtime error stating "NameError: name 'sampleProperties' is not defined". samplePropertyDict is a Dictionary and is being passed into the Constructor, as the print statements are kicking out the relevant information I'm passing through, so it's really puzzling to me as to why the program is claiming that sampleProperty is not defined.
class Sample:
sampleProperties = {}
def __init__(self, propertyList):
print("initializing new sample...")
sampleProperties = propertyList
print(sampleProperties)
def getProperty(self, propertyName):
for i in sampleProperties:
if(i.keys() == propertyName):
return i.values();
return "Not found"
tempSample = Sample(samplePropertyDict)
sampleList.append(tempSample) #this section is part of an earlier for loop, a bunch of samples are added into this list
for o in sampleList:
print(o.getProperty("Notable Sample ID"))
Error it's kicking:
Traceback (most recent call last):
File "D:\Sample Eligibility Generator\main.py", line 85, in <module>
print(o.getProperty("Notable Sample ID"))
File "D:\Sample Eligibility Generator\main.py", line 11, in getProperty
for i in sampleProperties:
NameError: name 'sampleProperties' is not defined
Print statements showing that sampleProperties did take in the values passed through the constructor: