Re: [Python-ideas] staticmethod and classmethod should be callable

2018-06-22 Thread Random832
On Thu, Jun 21, 2018, at 05:00, INADA Naoki wrote:
> When Python 4, I think we can even throw away classmethod and staticmethod
> object.
> PyFunction can have binding flag instead, like METH_CLASS and METH_STATIC
> for PyCFunction.
> classmethod and staticmethod is just a function which modify the flag.

I can't remember the details, but I remember once having a reason to need to 
use staticmethod to store an attribute which happened to be a function.
___
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] staticmethod and classmethod should be callable

2018-06-22 Thread Nick Coghlan
On 21 June 2018 at 03:27, Serhiy Storchaka  wrote:
> 20.06.18 20:07, Guido van Rossum пише:
>>
>> Maybe we're misunderstanding each other? I would think that calling the
>> classmethod object directly would just call the underlying function, so this
>> should have to call utility() with a single arg. This is really the only
>> option, since the descriptor doesn't have any context.
>>
>> In any case it should probably `def utility(cls)` in that example to
>> clarify that the first arg to a class method is a class.
>
>
> Sorry, I missed the cls parameter in the definition of utility().
>
> class Spam:
> @classmethod
> def utility(cls, arg):
> ...
>
> value = utility(???, arg)
>
> What should be passed as the first argument to utility() if the Spam class
> (as well as its subclasses) is not defined still?

That would depend on the definition of `utility` (it may simply not be
useful to call it in the class body, which is also the case with most
instance methods).

The more useful symmetry improvement is to the consistency of
behaviour between instance methods on class instances and the
behaviour of class methods on classes themselves.

So I don't think this is a huge gain in expressiveness, but I do think
it's a low cost consistency improvement that should make it easier to
start unifying more of the descriptor handling logic internally.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
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] staticmethod and classmethod should be callable

2018-06-21 Thread Brendan Barnwell

On 2018-06-21 01:33, Serhiy Storchaka wrote:

21.06.18 10:45, Jeroen Demeyer пише:

>On 2018-06-20 19:33, Serhiy Storchaka wrote:

>>20.06.18 12:56, Jeroen Demeyer пише:

>>>Are there any reasons to*not*  make staticmethod and classmethod
>>>callable?

>>
>>There were no reasons to make staticmethod and classmethod callable.

>
>You have to compare the advantages of making them callable vs. the
>advantages of*not*  making them callable.

You have also to weight the disadvantages of making them callable and
the cost of making them callable.


	That's what the OP is trying to do.  You were just asked if there were 
any disadvantages or costs.  Are there or not?  If so, what are they?


--
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."

   --author unknown
___
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] staticmethod and classmethod should be callable

2018-06-21 Thread Jeroen Demeyer

On 2018-06-21 11:00, INADA Naoki wrote:

When Python 4, I think we can even throw away classmethod and
staticmethod object.
PyFunction can have binding flag instead, like METH_CLASS and
METH_STATIC for PyCFunction.
classmethod and staticmethod is just a function which modify the flag.


One issue with that idea is that staticmethod, classmethod can actually 
arbitrary objects, not only Python functions. In fact, even this object 
can be created:


>>> staticmethod(42)


So in that sense, they behave more like "method" which can also wrap 
arbitrary callables (in this case, callability is checked). So I'm 
vaguely thinking of putting "method", "staticmethod" and "classmethod" 
on top of a common base class for things wrapping callables.



Jeroen.
___
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] staticmethod and classmethod should be callable

2018-06-21 Thread INADA Naoki
>
>
> My question is really: assuming that we redesign
> staticmethod/classmethod anyway, should we make them callable?
>

​I think so.  staticmethod and classmethod should affect descriptor
behavior.
And it should behave as normal function.​

>>> @classmethod
... def foo(cls):
... print(cls)
...
>>> @staticmethod
... def bar(arg):
... print(arg)
...
>>> foo(int)  # this should work
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'classmethod' object is not callable
>>> bar(42)  # this should work too
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'staticmethod' object is not callable


When Python 4, I think we can even throw away classmethod and staticmethod
object.
PyFunction can have binding flag instead, like METH_CLASS and METH_STATIC
for PyCFunction.
classmethod and staticmethod is just a function which modify the flag.

​But I'm not sure.  Calling in Python is too complicated ​to fully
understand.

​Regards,​
-- 
INADA Naoki  
___
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] staticmethod and classmethod should be callable

2018-06-21 Thread Jeroen Demeyer

On 2018-06-21 10:33, Serhiy Storchaka wrote:

Status quo wins.


Well, I'm already planning to make changes to staticmethod/classmethod 
(not right now, but it's on my post-PEP-580 roadmap). So the "status 
quo" argument doesn't apply.


My question is really: assuming that we redesign 
staticmethod/classmethod anyway, should we make them callable?

___
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] staticmethod and classmethod should be callable

2018-06-21 Thread Serhiy Storchaka

21.06.18 10:45, Jeroen Demeyer пише:

On 2018-06-20 19:33, Serhiy Storchaka wrote:

20.06.18 12:56, Jeroen Demeyer пише:
Are there any reasons to *not* make staticmethod and classmethod 
callable?


There were no reasons to make staticmethod and classmethod callable.


You have to compare the advantages of making them callable vs. the 
advantages of *not* making them callable.


You have also to weight the disadvantages of making them callable and 
the cost of making them callable.


I think that consistency *is* good to have, so I consider that one 
reason to make them callable. Are there any reasons for *not* making 
them callable?


Status quo wins.

___
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] staticmethod and classmethod should be callable

2018-06-21 Thread Jeroen Demeyer

On 2018-06-20 19:33, Serhiy Storchaka wrote:

20.06.18 12:56, Jeroen Demeyer пише:

Are there any reasons to *not* make staticmethod and classmethod callable?


There were no reasons to make staticmethod and classmethod callable.


You have to compare the advantages of making them callable vs. the 
advantages of *not* making them callable.


I think that consistency *is* good to have, so I consider that one 
reason to make them callable. Are there any reasons for *not* making 
them callable?

___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Guido van Rossum
Maybe. Though it has surprised me occasionally that pulling a classmethod
or staticmethod out of the class dict (like in Jeroen's original example)
doesn't work.

On Wed, Jun 20, 2018 at 10:34 AM Serhiy Storchaka 
wrote:

> 20.06.18 12:56, Jeroen Demeyer пише:
> > Are there any reasons to *not* make staticmethod and classmethod
> callable?
>
> There were no reasons to make staticmethod and classmethod callable.
> Just "for consistency" is not considered a good reason.
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Serhiy Storchaka

20.06.18 12:56, Jeroen Demeyer пише:

Are there any reasons to *not* make staticmethod and classmethod callable?


There were no reasons to make staticmethod and classmethod callable. 
Just "for consistency" is not considered a good reason.


___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Serhiy Storchaka

20.06.18 20:07, Guido van Rossum пише:
Maybe we're misunderstanding each other? I would think that calling the 
classmethod object directly would just call the underlying function, so 
this should have to call utility() with a single arg. This is really the 
only option, since the descriptor doesn't have any context.


In any case it should probably `def utility(cls)` in that example to 
clarify that the first arg to a class method is a class.


Sorry, I missed the cls parameter in the definition of utility().

class Spam:
@classmethod
def utility(cls, arg):
...

value = utility(???, arg)

What should be passed as the first argument to utility() if the Spam 
class (as well as its subclasses) is not defined still?



Maybe there is a use case for calling the staticmethod descriptor. 
Although in this rare case I would apply the staticmethod decorator 
after using the function in the class body.


class Spam:
# @staticmethod
def utility(arg):
...

value = utility(arg)
utility = staticmethod(utility)

But I don't see a use case for calling the classmethod descriptor.

___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Guido van Rossum
On Wed, Jun 20, 2018 at 10:03 AM Serhiy Storchaka 
wrote:

> 20.06.18 19:37, Guido van Rossum пише:
> > On Wed, Jun 20, 2018 at 9:31 AM Serhiy Storchaka
> >  > > wrote:
> >
> > 20.06.18 19:20, Guido van Rossum пише:
> >  > +1 -- when we introduced these we didn't see the use case so
> > clearly,
> >  > but it definitely exists.
> >
> > How would you call a classmethod descriptor in this case?
> >
> >
> > With an extra first argument that's a class -- it should just call the
> > wrapped function with whatever args are presented to the descriptior.
>
> This differs from calling a class method outside of the class definition
> body. And in the class definition body the class is not defined still.
>
> class Spam:
>  @classmethod
>  def utility(arg):
>  ...
>
>  value = utility(???, arg)
>

Maybe we're misunderstanding each other? I would think that calling the
classmethod object directly would just call the underlying function, so
this should have to call utility() with a single arg. This is really the
only option, since the descriptor doesn't have any context.

In any case it should probably `def utility(cls)` in that example to
clarify that the first arg to a class method is a class.

-- 
--Guido van Rossum (python.org/~guido)
___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Serhiy Storchaka

20.06.18 19:37, Guido van Rossum пише:
On Wed, Jun 20, 2018 at 9:31 AM Serhiy Storchaka 
> wrote:


20.06.18 19:20, Guido van Rossum пише:
 > +1 -- when we introduced these we didn't see the use case so
clearly,
 > but it definitely exists.

How would you call a classmethod descriptor in this case?


With an extra first argument that's a class -- it should just call the 
wrapped function with whatever args are presented to the descriptior.


This differs from calling a class method outside of the class definition 
body. And in the class definition body the class is not defined still.


class Spam:
@classmethod
def utility(arg):
...

value = utility(???, arg)

___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Guido van Rossum
On Wed, Jun 20, 2018 at 9:31 AM Serhiy Storchaka 
wrote:

> 20.06.18 19:20, Guido van Rossum пише:
> > +1 -- when we introduced these we didn't see the use case so clearly,
> > but it definitely exists.
>
> How would you call a classmethod descriptor in this case?
>

With an extra first argument that's a class -- it should just call the
wrapped function with whatever args are presented to the descriptior.

-- 
--Guido van Rossum (python.org/~guido)
___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Serhiy Storchaka

20.06.18 19:20, Guido van Rossum пише:
+1 -- when we introduced these we didn't see the use case so clearly, 
but it definitely exists.


How would you call a classmethod descriptor in this case?

___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Guido van Rossum
+1 -- when we introduced these we didn't see the use case so clearly, but
it definitely exists.

On Wed, Jun 20, 2018 at 4:44 AM Steven D'Aprano  wrote:

> On Wed, Jun 20, 2018 at 11:56:05AM +0200, Jeroen Demeyer wrote:
> [...]
> > Since it makes sense to merge the classes "classmethod" and
> > "classmethod_descriptor" (PEP 579, issue 8), one of the above behaviors
> > should be changed. Given that adding features is less likely to break
> > stuff, I would argue that classmethod instances should become callable.
> [...]
> > Are there any reasons to *not* make staticmethod and classmethod
> callable?
>
> (The classes themselves are callable -- you're talking about the
> instances.)
>
> +1 yes please!
>
> The fact that classmethods and especially staticmethod instances aren't
> callable has been a long-running niggling pain for me. Occasionally I
> want to do something like this:
>
> class Spam:
> @staticmethod
> def utility(arg):
> # something which is conceptually related to the Spam class
> # but doesn't need a cls/self argument.
> ...
>
> value = utility(arg)
>
> but it doesn't work as staticmethod objects aren't callable until after
> they've gone through the descriptor protocol.
>
>
> I'm not the only one bitten by this:
>
>
> https://stackoverflow.com/questions/45375944/python-static-method-is-not-always-callable
>
> https://mail.python.org/pipermail/python-list/2011-November/615069.html
>
> Part of that thread, see links and discussion here:
>
> https://mail.python.org/pipermail/python-list/2011-November/615077.html
>
>
> I thought I had raised a bug report for this on the tracker, but my
> google-fu is failing me and I can't find it. But my recollection is that
> the simple fix is to make staticmethod.__call__ simply delegate to the
> underlying decorated function. And similar for classmethod.
>
> (Of course calling classmethod instances directly won't work unless you
> provide the class argument. But that's just a simple matter of bound
> versus unbound methods.)
>
>
> --
> 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/
>


-- 
--Guido van Rossum (python.org/~guido)
___
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] staticmethod and classmethod should be callable

2018-06-20 Thread Steven D'Aprano
On Wed, Jun 20, 2018 at 11:56:05AM +0200, Jeroen Demeyer wrote:
[...]
> Since it makes sense to merge the classes "classmethod" and 
> "classmethod_descriptor" (PEP 579, issue 8), one of the above behaviors 
> should be changed. Given that adding features is less likely to break 
> stuff, I would argue that classmethod instances should become callable.
[...]
> Are there any reasons to *not* make staticmethod and classmethod callable?

(The classes themselves are callable -- you're talking about the 
instances.)

+1 yes please!

The fact that classmethods and especially staticmethod instances aren't 
callable has been a long-running niggling pain for me. Occasionally I 
want to do something like this:

class Spam:
@staticmethod
def utility(arg):
# something which is conceptually related to the Spam class
# but doesn't need a cls/self argument.
...

value = utility(arg)

but it doesn't work as staticmethod objects aren't callable until after 
they've gone through the descriptor protocol.


I'm not the only one bitten by this:

https://stackoverflow.com/questions/45375944/python-static-method-is-not-always-callable

https://mail.python.org/pipermail/python-list/2011-November/615069.html

Part of that thread, see links and discussion here:

https://mail.python.org/pipermail/python-list/2011-November/615077.html


I thought I had raised a bug report for this on the tracker, but my 
google-fu is failing me and I can't find it. But my recollection is that 
the simple fix is to make staticmethod.__call__ simply delegate to the 
underlying decorated function. And similar for classmethod.

(Of course calling classmethod instances directly won't work unless you 
provide the class argument. But that's just a simple matter of bound 
versus unbound methods.)


-- 
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/