python variable scope
- by Oscar Reyes
I'm teaching my self python and I was translating some sample
class Student:
def __init__( self, name, a,b,c ):
self.name = name
self.a = a
self.b = b
self.c = c
def average(self):
return ( a+b+c ) / 3.0
Which is pretty much my intended class definition
Later in the main method I create an instance and call it a
if __name__ == "__main__" :
a = Student( "Oscar", 10, 10, 10 )
That's how I find out that the variable a declared in main is available to the method average and that to make that method work , I have to type self.a + self.b + self.c instead
What's the rationale of this?
I found related questions, but I don't really know if they are about the same