Movatterモバイル変換
[0]ホーム
[Python-ideas] Fwd: Define a method or function attribute outside of a class with the dot operator
Chris Angelicorosuav at gmail.com
Fri Feb 10 06:05:30 EST 2017
On Fri, Feb 10, 2017 at 9:45 PM, Markus Meskanen<markusmeskanen at gmail.com> wrote:>> That saves one line, at the cost of introducing even more complexity to>> the language.>>>> Are these use-cases common enough to justify the extra syntax?>>> 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.>Every now and then, we get a proposal along these lines. I think it'sabout time a PEP got written.The usual way this is explained is that a function name can beanything you can assign to. Currently, a function has to have a simplename, and it then gets created with that as its __name__ and bound tothat name in the current namespace (module, class, or function). Toachieve what you're looking for, the syntax would be defined in termsof assignment, same as a 'for' loop's iteration variable is:# Perfectly legalfor spam.ham in iter: pass# Not currently legaldef ham.spam(): passMarkus, do you want to head this up? I'll help out with any editorialwork you have trouble with (as a PEP editor, I can assign it a numberand so on).Considerations:* What would the __name__ be? In "def ham.spam():", is the name "spam"or "ham.spam"? Or say you have "def x[0]():" - is the name "x[0]" orsomething else?* Are there any syntactic ambiguities? Any special restrictions?* Exactly what grammar token would be used? Currently NAME; might become 'test'?* Will there be any possible backward incompatibilities?Create a pull request againsthttps://github.com/python/peps - lookslike the next number is 542.Any questions, I'm happy to help.ChrisA
More information about the Python-ideasmailing list
[8]ページ先頭