Tieu Truc
Tieu Truc

Reputation: 21

How to fix: AttributeError:Class object has no attribute

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

Upvotes: 2

Views: 4678

Answers (1)

Ben
Ben

Reputation: 2472

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

Upvotes: 5

Related Questions