2

I am setting up a class, and as first step has used __init__ function to initialize the attributes. However when I try to create an instance from that class, it shows the AttributeError.

I have checked the codes again and again to see if there is anything wrong with the syntax but the error remains

class RandomWalk():
    def ___init___(self, points = 10):
        """initialize attributes of a walk"""
        self.points = points
        self.x_values = [0]
        self.y_values = [0]

rw = RandomWalk()
print(rw.points)

I expect the output of 10 as default value of points, but instead the error shows:

Traceback (most recent call last):
  File "test1.py", line 10, in <module>
    print(rw.points)
AttributeError: 'RandomWalk' object has no attribute 'points'

The issue remains if I replace attribute points with either x_values or y_values

Tieu Truc
  • 21
  • 1
  • 4
  • Check this out https://stackoverflow.com/questions/2681243/how-should-i-declare-default-values-for-instance-variables-in-python – marsnebulasoup Jul 30 '19 at 15:59

1 Answers1

5

You have extra underscores in your constructor, so it is not being called. Use two on either side: __init__.

Ben
  • 2,422
  • 2
  • 16
  • 23