[Python-ideas] Add .= as a method return value assignment operator

Steven D'Aprano steve at pearwood.info
Thu Sep 27 22:49:11 EDT 2018


On Thu, Sep 27, 2018 at 09:47:42PM -0400, James Lu wrote:
> > I agree that this adds ambiguity where we can't be sure whether text .= 
> > encode('utf-8') is referring to the function or the method. We can infer 
> > that it *ought* to be the method, and maybe even add a rule to force 
> > that, but this works only for the simple cases. It is risky and error 
> > prone in the hard cases.
>
> I think it would be a good idea to treat all global name lookups as 
> lookups on the object on the LHS when you’re on the RHS of .=.

We would be innundated with questions and bug reports complaining that

    # earlier...
    argument = something()

    # later on
    myobj = MyClass()
    myobj .= method(argument)

fails with 

    AttributeError: 'MyClass' object has no attribute 'argument'

and people would be right to complain. That rule would mean you couldn't 
use builtins or module-level names on the right hand side of the .= 
operator. I call that a huge misfeature.


> This behavior prevents the worst mistakes and makes it very clear what 
> is happening.

Far from preventing mistakes, it would cause them, whenever somebody 
intended to use a builtin or module-level object, and accidently got a 
completely different attribute of the target object.


-- 
Steve


More information about the Python-ideas mailing list