The right way to 'call' a class attribute inside the same class
rosuav at gmail.com
Mon Dec 12 18:23:57 EST 2016
On Tue, Dec 13, 2016 at 10:17 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> If the differences didn't matter I would agree that “overly pedantic” is
> fair. But those differences trip up newcomers. Thinking of
> ‘Foo.__init__’ leads people to wonder where the ‘self’ attribute came
> from – am I not meant to be constructing it? — and to attempt to return
> that instance. And when the time comes to lean about ‘__new__’ the
> confusion continues, because the newcomer has been told that something
> *else* is the constructor, so what's this?
constructor". When you do, there is a 'this' object before you start.
'this' should end up existing? Does it really even matter when memory
gets allocated and the object's identity assigned? Before __init__
gets called, the object isn't "truly there" - its fundamental
invariants may not yet have been established, and key attributes might
not have been set up. Once __init__ finishes, there is an expectation
that attributes and invariants are sorted out.
This is like the old "Python doesn't have variables" thing.
Ultimately, every language has slightly different semantics (otherwise
they'd be trivial transformations, like Ook and Brainf*), so you have
to learn that the "constructor" might have slightly different
semantics. Accept it - embrace it. Learn it.
More information about the Python-list