Why does this not work?
x = 100
print(self.x)
I understand that self
keyword refers to the variables in the current class. So I used this in the __main__
class, right? So why does it throw an error?
The error is as given below:
Traceback (most recent call last):
File "/home/kali/Documents/pythonClassPractice/test.py", line 3, in <module>
print(self.x)
^^^^
NameError: name 'self' is not defined
New contributor
Ankeet Saha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9