python variable scope

Posted by Oscar Reyes on Stack Overflow See other posts from Stack Overflow or by Oscar Reyes
Published on 2010-04-20T18:11:58Z Indexed on 2010/04/20 18:13 UTC
Read the original article Hit count: 635

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

© Stack Overflow or respective owner

Related posts about python

Related posts about newbie