[Python-ideas] Fwd: Define a method or function attribute outside of a class with the dot operator
markusmeskanen at gmail.com
Fri Feb 10 05:45:27 EST 2017
> 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.
> What advantage does this proposed syntax have?
> Since that's not actually a rhetorical question, I'll answer it myself:
> def Spam.method(self) not only saves the line
> Spam.method = method
> but it also avoids leaving a global name "method" in the namespace (no
> need to follow with `del method`); it makes it explicit from the
> beginning that the function is an attribute of Spam.
Yeah this is a major reason why I want this, and the reason I mentioned
"unnatural order" in the original mail. Having the class's name in the
beginning just makes it feel right..
> If the implementation is clever enough, it can avoid clashing with a
> global of the same name:
> eggs = "something"
> def Spam.eggs(self):
> def Cheese.eggs(self):
> assert eggs == "something"
> So the idea isn't without merit; but the question in my mind is whether
> the advantages outweigh the extra complexity.
I didn't even realize you would avoid the global namespace issue too, this
makes me fall in love with the idea even more. I really think the added
complexity isn't much. One thing to consider, however, is what would happen
if someone attempted to use this in a class definition:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas