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

2017-02-11 Thread Steven D'Aprano
On Fri, Feb 10, 2017 at 06:17:54PM +0200, Markus Meskanen wrote:
> Well yes, but I think you're a bit too fast on labeling it a mistake to use
> monkey patching...

More importantly, I think we're being a bit too quick to label this 
technique "monkey-patching" at all. Monkey-patching (or MP for brevity) 
implies making modifications to some arbitrary *uncooperative* class (or 
instance). When you're plugging electrodes into a monkey's brain, the 
monkey has no say in it.

This proposed syntax can, of course, be used that way, but Python is 
already a "consenting adults" language and already has setattr:

setattr(some_class, 'get_shrubbery', get_shrubbery)

which is all you need to enable MP for good or evil.

There have been a few times where I would have used this syntax if it 
had been available, and none of them were MP. They were injecting 
methods into classes I controlled.

I suspect that this technique wouldn't feel so bad if we had a proper, 
respectable sounding "design pattern" name for it, like "method 
injection" or something. I expect that the only reason there is no name 
for this is that Java doesn't allow it. (I think.) So I'm going to call 
it "method injection".

I don't think there's any evidence that slightly cleaner syntax for 
method injection will encourage MP. We already have clean syntax to 
inject arbitrary attributes (including methods made with lambda):

TheClass.method = lambda self: ...

and I don't think there's an epidemic of MP going on.



-- 
Steve
___
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 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 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
mailto:markusmeska...@gmail.com>> 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




___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


___
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 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
___
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 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 them with better tools?

Because encouraging and making it easier for people to make mistakes
is the wrong thing to do, surely?

Paul
___
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 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  wrote:

> 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
> about how it should be avoided.)
>
> My favourite description of object-oriented programming is that it's like
> "reading a road map through a drinking(/soda/pop) straw". We do not need to
> tell people that it's okay to make this problem worse by providing
> first-class tools to do it.
>
> Top-posted from my Windows Phone
> --
> From: Chris Angelico 
> Sent: ‎2/‎10/‎2017 8:27
> To: Python-Ideas 
> Subject: Re: [Python-ideas] Fwd: Define a method or function
> attributeoutside of a class with the dot operator
>
> 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 if you attempt to use zero-argument super() when *not* at
> > class scope?
> >
> > These are *answerable* questions...
>
> ... and are exactly why I asked the OP to write up a PEP. This isn't
> my proposal, so it's not up to me to make the decisions.
>
> 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 on that __name__.
>
> Zero-argument super() would behave exactly the way it would if you
> used a simple name. This just changes the assignment, not the creation
> of the function. So if you're inside a class, you could populate a
> lookup dictionary with method-like functions. Abuse this, and you're
> only shooting your own foot.
>
> Zero-argument super() outside of a class, just as currently, would be
> an error. (Whatever kind of error it currently is.)
>
> Maybe there are better answers to these questions, I don't know.
> That's what the PEP's for.
>
> ChrisA
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
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 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 about how it 
should be avoided.)

My favourite description of object-oriented programming is that it's like 
"reading a road map through a drinking(/soda/pop) straw". We do not need to 
tell people that it's okay to make this problem worse by providing first-class 
tools to do it.

Top-posted from my Windows Phone

-Original Message-
From: "Chris Angelico" 
Sent: ‎2/‎10/‎2017 8:27
To: "Python-Ideas" 
Subject: Re: [Python-ideas] Fwd: Define a method or function attributeoutside 
of a class with the dot operator

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 if you attempt to use zero-argument super() when *not* at
> class scope?
>
> These are *answerable* questions...

... and are exactly why I asked the OP to write up a PEP. This isn't
my proposal, so it's not up to me to make the decisions.

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 on that __name__.

Zero-argument super() would behave exactly the way it would if you
used a simple name. This just changes the assignment, not the creation
of the function. So if you're inside a class, you could populate a
lookup dictionary with method-like functions. Abuse this, and you're
only shooting your own foot.

Zero-argument super() outside of a class, just as currently, would be
an error. (Whatever kind of error it currently is.)

Maybe there are better answers to these questions, I don't know.
That's what the PEP's for.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/