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

Paul Moore p.f.moore at gmail.com
Fri Feb 10 06:13:51 EST 2017

On 10 February 2017 at 10:45, Markus Meskanen <markusmeskanen at gmail.com> wrote:
> Keep in mind that the extra syntax is *very* minor, and goes hand-to-hand
> with the existing attribute access syntax. Basically it's taking the
> existing syntax to one more place, where it in my opinion should have been
> since long ago.

In implementation terms, the syntax change is not as minor as you
suggest. At the moment, the syntax for a "def" statement is:

funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->"
expression] ":" suite
funcname  ::=  identifier

You're proposing replacing "identifier" as the definition of a
"funcname" with... what? dotted_name might work, but that opens up the
possibility of

class Foo:

foo = Foo

def foo.a(self): pass

(note I'm defining a method on the *instance*, not on the class). Do
you want to allow that? What about "def a.b.c.d.e(): pass" (no self as
argument, deeply nested instance attribute).

Furthermore, once we open up this possibility, I would expect requests
for things like

    func_table = {}
    func_table["foo"] = lambda a, b: a+b
    def func_table["bar"] (a,b):
        return a-b

pretty quickly. How would you respond to those? (Setting up function
tables is a much more common and reasonable need than monkeypatching

Your proposal is clear enough in terms of your intent, but the
implementation details are non-trivial.


PS Personally, I'm slightly in favour of the idea in principle, but I
don't think it's a useful enough addition to warrant having to deal
with all the questions I note above.

More information about the Python-ideas mailing list