I have no class

Dan Sommers dan at tombstonezero.net
Mon Nov 24 14:44:18 CET 2014


On Mon, 24 Nov 2014 16:11:32 +1100, Chris Angelico wrote:

> On Mon, Nov 24, 2014 at 3:21 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Sun, 23 Nov 2014 09:02:57 -0800, Rustom Mody wrote:
>>
>>> Python is a bit odd in the OO-world in that it prioritizes "Explicit is
>>> better than implicit" over convenience.
>>>
>>> Notice that you use self.throw where in most other OOP languages you
>>> would use just throw.
>>
>> I don't think that is correct. I think that most OOP languages are like
>> Python, and use a special variable to reference the current instance:
>>
>> In some of these languages, the use of "this/self/me" is optional, but
>> I'm not aware of *any* OOP language where there is no named reference to
>> the current object at all.
> 
> I believe his point is that Python, unlike every other language he can
> think of, requires "self.x" instead of just "x". Every language needs
> a way to say "current object", but not every language needs you to say
> that for every member reference. C++, Pike, and Java let you
> short-hand that. (They're all deriving from the same syntactic style
> anyway.) JavaScript doesn't, I believe, although its variable scoping
> rules are some of the most insane I've ever met, so there might be a
> way to shortcut it. If your experience of OO is mainly from C++/Java
> family languages, then yes, Python will seem odd.

And then, at least in C++, you see coding standards that demand that
member variables (aka instance variables aka attributes) be named in
such a way that you can tell that they're member variables and not local
variables (one common convenstion is that all member variables begin
with "m_").  IMO, that's a concession that all that implicitness and
convenience just causes confusion.  In Python, the names of all instance
attributes begin with "self."

Dan



More information about the Python-list mailing list