-1

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: enter image description here

GoldenLyfe
  • 49
  • 1
  • 1
  • 4
  • If you intend `sampleProperties` to be a class variable that is shared among all `Sample` instances, then refer to it using `Sample.sampleProperties`. – John Gordon May 18 '20 at 21:18
  • There are probably thousands of questions addressing this, and even a quick perusal of the the python [tutorial on class definitions](https://docs.python.org/3/tutorial/classes.html) would have cleared this up immediately. Please put in a modicum of effort on researching the problem yourself before asking a question. – juanpa.arrivillaga May 18 '20 at 21:18
  • Note, you are defining a *class-variable* (i.e. static variable) `sampleProperties = {}` which you always try to shadow in the constructor... don't do that. Just remove that line. – juanpa.arrivillaga May 18 '20 at 21:26

2 Answers2

1

You should use instance variables in the constructor.

class Sample:
    def __init__(self, propertyList):
        print("initializing new sample...")
        self.sampleProperties = propertyList
        print(self.sampleProperties)
    def getProperty(self, propertyName):
        for i in self.sampleProperties:
            if(i.keys() == propertyName):
                return i.values();
        return "Not found"

You are getting the error because you are trying to access a variable which is not defined in that scope. Using instance variables makes it so that you can always access the variables using self.myVar.

peterxz
  • 864
  • 1
  • 6
  • 23
0

Use it like self.sampleProperties within the class.

ngShravil.py
  • 4,742
  • 3
  • 18
  • 30