[Python-ideas] Fwd: Define a method or function attribute outside of a class with the dot operator

Chris Angelico rosuav at gmail.com
Sat Feb 11 23:50:03 EST 2017

On Sun, Feb 12, 2017 at 3:38 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Feb 11, 2017 at 06:33:39PM +1300, Greg Ewing wrote:
>> Chris Angelico wrote:
>> >Which is why these proposals always seem to gravitate to "anything you
>> >can assign to",
>> There might be some parsing difficulties with that, e.g.
>>    def foo(x)[5](y, z):
>>       ...
>> That should be acceptable, because foo(x)[5] is something
>> assignable, but foo(x) looks like the beginning of the
>> definition of a function called foo. I'm not sure whether
>> the parser would cope with that.
> Forget the parser. I know *I* can't cope with that.
> *wink*

So you think the language should prevent silly assignments?

>>> stuff = [None] * 10
>>> def foo(): return stuff
>>> for x, foo()[x] in enumerate(range(len(stuff))): pass
>>> stuff
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Given that Python is happy to do these kinds of assignments in 'for'
statements, I don't see any reason to prevent them in 'def'
statements. It's not the language's job to prevent abuse; at best,
that's a job for a style guide.


More information about the Python-ideas mailing list