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

1

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.