Semantics question...

Joshua Macy amused at webamused.com
Tue May 16 19:19:39 EDT 2000


David Allen wrote:
> 
> class Foobar:
>         def bar(self, someNumber):
>                 self.internal_data = someNumber
>                 return(None)
> 
> why is it that python requires programmers to refer to class data by using the
> object?  (as in, you have to say self.internal_data = someNumber.  You could
> just say internal_data = someNumber but it wouldn't do what you wanted it to)
> 
> 

  Essentially, to make instance members visually distinct from method
locals.  The other OO languages you're thinking of put an implicit
"this." in front of (some) accesses within a method, leading to
confusion (at least sometimes) about to whom internal_data belongs.  OO
programmers in those languages will then resort to things like naming
conventions (e.g. preceding all local names with m_) to help them
remember. Python's design disfavors such under the covers "help", so
requires that you explicitly pass in a reference to the object as the
first parameter (traditionally called "self"), and fully qualify member
access.

 Joshua



More information about the Python-list mailing list