0

I have a very simple program in python to check if a list has a zero value in it

class ZeroList(object): 
   def __init__(self,coordinates):
       self.coordinates = coordinates

   def is_zero(value):
       return math.isclose(value,0)

   def test(self):  
    for coord in self.coordinate:
        if not is_zero(coord):
            print(coord)    
a = ZeroList([1,2,0,8,5,0,3,7,0,3])
a.test()

Yet running this results in the error

Traceback (most recent call last):
 File "line2.py", line 2, in <module>
  class Test(object):
  File "line2.py", line 12, in Test
    test([1,2,3])
   File "line2.py", line 8, in test
     if not is_zero(i):
  NameError: name 'is_zero' is not defined

The method is_zero is clearly defined so why is this error showing up?

Zach
  • 1
  • 1
  • Note that the solution from the duplicate I linked won't work for you because there is a bunch of other mistakes in your code. This is not how classes work. Please take some time to read a good tutorial about OOP. – Aran-Fey May 24 '18 at 18:31
  • I'm coming from mainly using c# the past several years where I'm very familiar with OOP in that language, but the differences in Python have been throwing me for a loop. From your link, I can see I that I need to use the self keyword, but calling self.is_zero() results in an AttributeError saying the object has no attribute 'is_zero'. Can you clarify the bunch of other mistakes I'm making so I can narrow it down? – Zach May 24 '18 at 18:58
  • First of all, you never create an instance of your class, because you've dropped the `test([1,2,3])` call directly in the class scope. In other words, you're calling the `test` method with `self` set to `[1,2,3]`. Other than that, I can't really say what's a mistake and what isn't - I honestly can't tell what your class is even supposed to do. With `for i in self:` you're using the class like an iterable. With `math.isclose(self,0)` you're using the class like a number. It really doesn't make any sense. – Aran-Fey May 24 '18 at 19:04
  • I appreciate your help. I'll admit, it was messy piece of code. I want to loop through a list and print only the non-zero values. I tried to clean up the code, but the issue still remains - I get an error saying the function is not defined when I defined it above. EDIT: I edited my original post with some cleaner code – Zach May 24 '18 at 22:29
  • Your class still doesn't make a whole lot of sense. Why doesn't the `is_zero` method have a `self` parameter? It looks a lot like you shouldn't be using a class in the first place. Or at least the `is_zero` function shouldn't be a part of the class. But anyway, as long as it's defined in the class, you have to call it as `self.is_zero()`. – Aran-Fey May 24 '18 at 22:33
  • I didn't plan on passing the entire list to 'is_zero', just the individual values to check if they are == zero. If I call 'self.is_zero()', I get a 'TypeError: must be a real number, not ZeroList' instead of a NameError – Zach May 24 '18 at 22:44
  • So if `is_zero` doesn't operate on instances of the class, move it out of the class. – Aran-Fey May 24 '18 at 22:47
  • That solved the issue, thanks again for helping out a novice. I think I understand now that any method that doesn't operate on an instance of the class and doesn't have `self` parameter should be placed outside of the class – Zach May 25 '18 at 00:20

0 Answers0