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

2017-02-10 Thread Markus Meskanen
I'm suggesting the addition of support to using a dot notation when defining a function to be a method of a class, or a callback attribute. For example: def foo(self): pass Foo.foo = foo Becomes: def Foo.foo(self): pass Other syntaxes can also be used if the dot itse

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

2017-02-10 Thread Steven D'Aprano
On Fri, Feb 10, 2017 at 11:13:47AM +0200, Markus Meskanen wrote: > I'm suggesting the addition of support to using a dot notation when > defining a function to be a method of a class, or a callback attribute. For > example: > > def foo(self): > pass > Foo.foo = foo > > Becomes: >

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

2017-02-10 Thread Matthias welp
Hi Markus, Thanks for writing this up, as I've had this same very valid problem before. On 10 February 2017 at 10:13, Markus Meskanen wrote: > I'm suggesting the addition of support to using a dot notation when defining > a function to be a method of a class, or a callback attribute. Your solut

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

2017-02-10 Thread Stephan Houben
What about using a simple decorator instead? def monkey_patch(cls): return lambda func: setattr(cls, func.__name__, func) class Foo: pass @monkey_patch(Foo) def bar(self): return 42 Foo().bar() # gives 42 2017-02-10 11:15 GMT+01:00 Matthias welp : > Hi Markus, > > Thanks for writin

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

2017-02-10 Thread Markus Meskanen
On Fri, Feb 10, 2017 at 12:29 PM, Stephan Houben wrote: > What about using a simple decorator instead? > > def monkey_patch(cls): > return lambda func: setattr(cls, func.__name__, func) > > class Foo: >pass > > @monkey_patch(Foo) > def bar(self): > return 42 > > Foo().bar() > # gives

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

2017-02-10 Thread Markus Meskanen
> > 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 takin

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

2017-02-10 Thread Chris Angelico
On Fri, Feb 10, 2017 at 9:45 PM, Markus Meskanen 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 >

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

2017-02-10 Thread Paul Moore
On 10 February 2017 at 10:45, Markus Meskanen 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

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

2017-02-10 Thread Chris Angelico
On Fri, Feb 10, 2017 at 10:13 PM, Paul Moore wrote: > 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 yo

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

2017-02-10 Thread Paul Moore
On 10 February 2017 at 11:16, Chris Angelico wrote: > On Fri, Feb 10, 2017 at 10:13 PM, Paul Moore wrote: >> 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"]

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

2017-02-10 Thread Steven D'Aprano
On Fri, Feb 10, 2017 at 10:05:30PM +1100, Chris Angelico wrote: > * What would the __name__ be? In "def ham.spam():", is the name "spam" > or "ham.spam"? "spam" of course, just like it is now: py> class Ham: ... def spam(self): ... ... ... py> py> Ham.spam.__name__ 'spam' You

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

2017-02-10 Thread Thomas Kluyver
On Fri, Feb 10, 2017, at 11:05 AM, Chris Angelico wrote: > * 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]" or > something else? I'd say 'spam' in the first case, and a special value like '' in the latter. Yo

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

2017-02-10 Thread Chris Angelico
On Fri, Feb 10, 2017 at 11:02 PM, Thomas Kluyver wrote: > On Fri, Feb 10, 2017, at 11:05 AM, Chris Angelico wrote: >> * 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]" or >> something else? > > I'd say 'spam'

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

2017-02-10 Thread Joao S. O. Bueno
I am definetelly -1 to this idea. But since you are discussing this seriously, one nice thing is to recall how Javascript does that: `function () ` is an expression that returns the created function, and thus can be assigned to anything on the left side. Of course, that would throw us back to a

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

2017-02-10 Thread Markus Meskanen
I've started working on a PEP for this since most people seem to be for it. Will see how it turns out. On Fri, Feb 10, 2017 at 2:13 PM, Joao S. O. Bueno wrote: > I am definetelly -1 to this idea. > > But since you are discussing this seriously, one nice thing is to > recall how Javascript does t

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

2017-02-10 Thread Steven D'Aprano
On Fri, Feb 10, 2017 at 02:28:25PM +0200, Markus Meskanen wrote: > I've started working on a PEP for this since most people seem to be for it. I don't know how you get "most people" -- there's only been a handful of responses in the few hours since the original post. And apart from one explicit

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

2017-02-10 Thread Stephan Houben
Hi all, I would like to add one more generic remark about syntax extensions, regarding something Markus said and which has bothered me before, also related to other syntax proposals. "Decorator approach is no different from doing `Foo.bar = bar` under the function definition I think, except it re

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

2017-02-10 Thread Markus Meskanen
> > > I've started working on a PEP for this since most people seem to be for > it. > > I don't know how you get "most people" -- there's only been a handful of > responses in the few hours since the original post. And apart from one > explicit -1, I read most of them as neutral, not in favour. >

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

2017-02-10 Thread Paul Moore
On 10 February 2017 at 13:55, Stephan Houben wrote: > My point would be that the new syntax *also* requires one to figure out what > the new syntax does. This is an extremely good point. It is mentioned when new syntax is proposed (the term often used is "discoverability") but the idea never seem

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

2017-02-10 Thread Paul Moore
On 10 February 2017 at 14:00, Markus Meskanen wrote: >> > I've started working on a PEP for this since most people seem to be for >> > it. >> >> I don't know how you get "most people" -- there's only been a handful of >> responses in the few hours since the original post. And apart from one >> exp

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

2017-02-10 Thread Markus Meskanen
> > I would like to add one more generic remark about syntax extensions, > regarding something > Markus said and which has bothered me before, also related to other syntax > proposals. > > "Decorator approach is no different from doing `Foo.bar = bar` under the > function definition I think, except

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

2017-02-10 Thread Nick Coghlan
On 10 February 2017 at 12:16, Chris Angelico wrote: > On Fri, Feb 10, 2017 at 10:13 PM, Paul Moore wrote: >> 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"]

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

2017-02-10 Thread Chris Angelico
On Sat, Feb 11, 2017 at 1:16 AM, Nick Coghlan wrote: > But what do __name__ and __qualname__ get set to? > > What happens if you do this at class scope, rather than at module > level or inside another function? > > What happens to the zero-argument super() support at class scope? > > What happens

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

2017-02-10 Thread Guyzmo via Python-ideas
Hi list, I'm quite neutral to this proposition, as it's not a use case I see often myself needing. On Fri, Feb 10, 2017 at 02:55:31PM +0100, Stephan Houben wrote: […] > But if I don't understand the dot in `class foo.bar:', then what? > It's probably somewhere buried in the language spec for `cla

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

2017-02-10 Thread Kyle Lahnakoski
On 2017-02-10 05:44, Markus Meskanen wrote: > > > On Fri, Feb 10, 2017 at 12:29 PM, Stephan Houben > wrote: > > What about using a simple decorator instead? > > def monkey_patch(cls): > return lambda func: setattr(cls, func.__name__, func) > I suggest

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

2017-02-10 Thread Steven D'Aprano
On Sat, Feb 11, 2017 at 01:25:40AM +1100, Chris Angelico wrote: > For what it's worth, my answers would be: > > __name__ would be the textual representation of exactly what you typed > between "def" and the open parenthesis. __qualname__ would be built > the exact same way it currently is, based

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

2017-02-10 Thread Chris Angelico
On Sat, Feb 11, 2017 at 2:25 AM, Steven D'Aprano wrote: > If I'm reading this right, you want this behaviour: > > class Spam: > pass > > def Spam.func(self): pass > > assert 'Spam.func' not in Spam.__dict__ > assert 'func' in Spam.__dict__ > > assert Spam.func.__name__ == 'Spam.func' > assert

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

2017-02-10 Thread Steve Dower
Since votes seem to be being counted and used for debate purposes, I am -1 to anything that encourages or condones people adding functionality to classes outside of the class definition. (Monkeypatching in my mind neither condones or encourages, and most descriptions come with plenty of caveats

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

2017-02-10 Thread Markus Meskanen
But if people are gonna do it anyways with the tools provided (monkey patching), why not provide them with better tools? And this wouldn't only be for classes, but for setting instance attributes too (see the Menu example in original mail). - Markus On Fri, Feb 10, 2017 at 5:38 PM, Steve Dower w

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

2017-02-10 Thread Markus Meskanen
Well yes, but I think you're a bit too fast on labeling it a mistake to use monkey patching... On Feb 10, 2017 18:15, "Paul Moore" wrote: On 10 February 2017 at 16:09, Markus Meskanen wrote: > But if people are gonna do it anyways with the tools provided (monkey > patching), why not provide th

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

2017-02-10 Thread Paul Moore
On 10 February 2017 at 16:09, Markus Meskanen wrote: > But if people are gonna do it anyways with the tools provided (monkey > patching), why not provide them with better tools? Because encouraging and making it easier for people to make mistakes is the wrong thing to do, surely? Paul __

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

2017-02-10 Thread Sven R. Kunze
Another point of view: Some call it monkeypatching. Others call it configuration. There's room for both views and I don't see anything wrong with configuration using this kind of feature. Sven On 10.02.2017 17:17, Markus Meskanen wrote: Well yes, but I think you're a bit too fast on labeli

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

2017-02-10 Thread Nick Coghlan
On 10 February 2017 at 16:25, Steven D'Aprano wrote: > On Sat, Feb 11, 2017 at 01:25:40AM +1100, Chris Angelico wrote: > >> For what it's worth, my answers would be: >> >> __name__ would be the textual representation of exactly what you typed >> between "def" and the open parenthesis. __qualname__

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

2017-02-10 Thread Gerald Britton
This is looking familiar. .Net extension methods anyone? ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Fwd: Define a method or function attributeoutsideof a class with the dot operator

2017-02-10 Thread Steve Dower
When you apply the "what if everyone did this" rule, it looks like a bad idea (or alternatively, what if two people who weren't expecting anyone else to do this did it). Monkeypatching is fairly blatantly taking advantage of the object model in a way that is not "supported" and cannot behave we

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

2017-02-10 Thread Joshua Morton
One thing that I don't think has been mentioned, but that brings me from a +0 to a more negative outlook, is the interaction between this proposal and some of python's existing class-related features, metaclasses and descriptors. That is currently we know that function definition, and even method d

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

2017-02-10 Thread Nick Timkovich
If everything was contained right in the same file, this is sanctioning another way to do it (when there should only be one obvious way). If you have multiple modules/packages, horrors can evolve where a class method could be patched in an unknown location by any loaded module (or you could even in

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

2017-02-10 Thread Markus Meskanen
Please keep in mind that this idea was not created to improve monkey patching, it just happens to be one of the side effects due to classes being objects. The main use case is the ability to set an instance's callback function (see the Menu example), and to allow the class being referenced in the f

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

2017-02-10 Thread Ethan Furman
On 02/10/2017 10:48 AM, Nick Timkovich wrote: If everything was contained right in the same file, this is sanctioning another way to do it (when there should only be one obvious way). No worries, this way is not obvious. If you have multiple modules/packages, horrors can evolve where a cla

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

2017-02-10 Thread Stephan Houben
Hi all, For what it's worth, I believe that the "class extension" scenario from Nick can be supported using plain ol' metaclasses. Not sure if this covers all desired capabilities, but at least the super() mechanism works correctly. Syntax is like this: class Foo(metaclass=class_extend(Foo)):

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

2017-02-10 Thread Stephan Hoyer
On Fri, Feb 10, 2017 at 9:20 AM, Nick Coghlan wrote: > What I would personally hope to see from the proposal is that given: > > class Spam: > pass > > def Spam.func(self): > return __class__ > > the effective runtime behaviour would be semantically identical to: > > cl

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

2017-02-10 Thread Steve Dower
On 10Feb2017 1400, Stephan Hoyer wrote: An important note is that ideally, we would still have way of indicating that Spam.func should exists in on the Spam class itself, even if it doesn't define the implementation. I suppose an abstractmethod overwritten by the later definition might do the tri

Re: [Python-ideas] Using Python for end user applications

2017-02-10 Thread Michel Desmoulin
This could change when webassembly is stable. If we manage to make a Python => webassembly compiler, I doubt it will make Python in the browser happen. But it certainly can make Python in NodeJS happen, and so in Electron apps. Le 09/02/2017 à 19:56, Nick Coghlan a écrit : > On 7 February 2017 a

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

2017-02-10 Thread Terry Reedy
On 2/10/2017 4:13 AM, Markus Meskanen wrote: I'm suggesting the addition of support to using a dot notation when defining a function to be a method of a class, or a callback attribute. My default starting position for every proposed syntax addition is -1. 1. Additions usually make Python more