Changing _init_ defined attribute in another class method Python

58 views Asked by At

I am new to Python , so this might be a stupid question. I wrote this :

Class A:
    def _init_(self):
        self.var1 = 2

    def update(self):
        self.var1 = 3 

But constantly getting the error in PyCharm : instance attribute var1 is defined outside init. Why?

1

There are 1 answers

2
Ranganathan On

The reason you are getting the error is due to typo error. Please replace '_' with double underscore while writing init() and similar functions. Correct code is as follows :

class A:
    def __init__(self):
        self.var1 = 3
    def update(self):
        self.var1 = 4