w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python - should class attributes be passed to class methods as local variables?

OK, I get it now. Thanks to David for his comments.

In the book I have been reading on classes, it says class methods need to have self added, but it doesn't explain why. When I saw references in the method referring to self.a or self.b, I assumed it was making references directly to the instance namespace, because I hadn't thought about the implications of passing an object called self to the method. So the references to the object are passed into the local namespace, and the method is making local references, which is consistent with the behavior of functions in the global namespace.

It was helpful just to ask the question - sorry if I wasted anyone's time reading it.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.