[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)
Brendan Barnwell
brenbarn at brenbarn.net
Sun May 14 13:48:01 EDT 2017
On 2017-05-14 00:34, Chris Angelico wrote:
> On Sun, May 14, 2017 at 2:53 PM, Brendan Barnwell <brenbarn at brenbarn.net> wrote:
>> Attributes aren't just for passing things to other methods. They're
>> for storing state. In your proposed system, how would an object mutate one
>> of its own attributes? It looks like "x" here is just stored in a function
>> closure, which wouldn't allow easy mutation. Also, how would another object
>> access the attribute from outside (as we currently do with self.x)? You can
>> say we'd only use this new attribute-free approach when we want to pass a
>> constructor argument that's used but never mutated or accessed from outside,
>> but that severely restricts the potential use cases, and all it saves you is
>> typing "self".
>
> My expectation is that you'd be using "nonlocal x" to do that.
That would allow mutation from within methods, but (as far as I can
tell) not access (or mutation) from outside the class.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown
More information about the Python-ideas
mailing list