Re: [Python-Dev] Making staticmethod objects callable?
Guido van Rossum wrote: There's no need to change Python so that people coming from other languages won't make silly mistakes, is there? Is that really a mistake... Yes, it's a mistake since staticmethod is a descriptor, but isn't it in a sense an implementation detail, particularly for a newbie? As Steven pointed, it is forcing to learn about descriptors. But I don't feel comfortable with that; I've always seen Python as a language that you can use with minimal knowledge. Again, I'm not thinking about anyone on this list. BTW I question the claimed reflex -- assuming by other languages you mean Java or C++ (the only languages I know that *have* static methods) -- since those languages don't have the ability to call methods (static or otherwise) at class definition time. Java, C++, C#. Yes, you're right, but the way I see it the first thing you learn in Python is that everything is dynamic. So I understand the reflex to quickly adapt the way you code to the new capabilities of Python. So perhaps you need to dig deeper to find out *why* this is a recurring issue. I think I understand why this is a recurring issue, but maybe I'm not good at explaining why. In the end, on that specific issue I think there's something to improve for newbies. The error message 'staticmethod' object is not callable could also be changed to something like 'staticmethod' object is a descriptor and is not callable. Personally, I prefer the it just works to ease your life compromise (it doesn't hurt much, no?). Note that it's not a big deal anyway and I hope it doesn't look like I want to argue; I just want to explain the issue. Regards, Nicolas ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
Armin Rigo wrote: Hi Nicolas, On Thu, Mar 02, 2006 at 01:55:03AM -0500, Nicolas Fleury wrote: (...) A use case is not hard to imagine, especially a private static method called only to build a class attribute. Uh. I do this all the time, and the answer is simply: don't make that a staticmethod. Staticmethods are for the rare case where you need dynamic class-based dispatch but don't have an instance around. I think we all agree on this list that there's no point using a staticmethod for that use case. My suggestion was for some comp.lang.python people, a lot coming from other languages. Their reflex would be much more to define a staticmethod. This issue has been pointed a lot of times on comp.lang.python. Regards, Nicolas ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
On 3/15/06, Nicolas Fleury [EMAIL PROTECTED] wrote: I think we all agree on this list that there's no point using a staticmethod for that use case. My suggestion was for some comp.lang.python people, a lot coming from other languages. Their reflex would be much more to define a staticmethod. This issue has been pointed a lot of times on comp.lang.python. There's no need to change Python so that people coming from other languages won't make silly mistakes, is there? BTW I question the claimed reflex -- assuming by other languages you mean Java or C++ (the only languages I know that *have* static methods) -- since those languages don't have the ability to call methods (static or otherwise) at class definition time. So perhaps you need to dig deeper to find out *why* this is a recurring issue. -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
Hi Nicolas, On Thu, Mar 02, 2006 at 01:55:03AM -0500, Nicolas Fleury wrote: (...) A use case is not hard to imagine, especially a private static method called only to build a class attribute. Uh. I do this all the time, and the answer is simply: don't make that a staticmethod. Staticmethods are for the rare case where you need dynamic class-based dispatch but don't have an instance around. class A: def _myinitializer(): do strange stuff here _myinitializer() del _myinitializer # optional A bientot, Armin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
On 3/12/06, Armin Rigo [EMAIL PROTECTED] wrote: Staticmethods are for the rare case where you needdynamic class-based dispatch but don't have an instance around.Actually, I would argue that's what classmethods are for, not staticmethods. You may not envision a desire for having the class in the method right now, but it won't hurt, either. The only real use-case for staticmethods that I know of is one Jp Caldrone pointed out once: storing arbitrary callables as class attributes: class MailHandleThingy(object): sendmail = mymaillib.mailsend ...Without wrapping that in a staticmethod, 'sendmail' may or may not become a bound method -- and that might change without touching any of the MailHandleThingy code. All cases where the callable is under your direct control, it's more rewarding (same buck, way more bang) to use classmethods, IMHO.(And no, calling a function during class-definition isn't a usecase for staticmethods :) -- Thomas Wouters [EMAIL PROTECTED]Hi! I'm a .signature virus! copy me into your .signature file to help me spread! ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Making staticmethod objects callable?
Hi, I've posted this question on comp.lang.python, but nobody seems to conclude it is a bad idea, so I post it here. http://groups.google.com/group/comp.lang.python/browse_frm/thread/6082dae1deef9161/88bb8a26750dd8c6?lnk=raothl=en#88bb8a26750dd8c6 Basically, should staticmethods be made callable so that the following would not raise an exception: class A: @staticmethod def foo(): pass bar = foo() There's workarounds, but it's really just about usability. staticmethod could still return a descriptor, but additionnally callable. Is there something I'm missing? Is it error-prone in any way? Note that I'm not a big fan of static methods myself, but I understand users expecting this work. Regards, Nicolas ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
On 3/1/06, Nicolas Fleury [EMAIL PROTECTED] wrote: Basically, should staticmethods be made callable so that the following would not raise an exception: class A: @staticmethod def foo(): pass bar = foo() There's workarounds, but it's really just about usability. staticmethod could still return a descriptor, but additionnally callable. Is there something I'm missing? Is it error-prone in any way? My only (mild) concern is that if staticmethod is going to get a __call__, I think classmethod should probably get one too. Inside a class this doesn't make much sense: class A(object): @classmethod def foo(cls): pass bar = foo(None) # ?? But I guess outside of a class maybe it's okay: @classmethod def foo(cls): pass class A(object): pass foo(A) Anyway, my feeling was that running into this behavior (that staticmethod is not callable) is a good oportunity to explain how descriptors work. And once you start playing around with staticmethod and classmethod, you're going to need to learn that pretty soon anyway. Hiding it a little bit longer with a __call__ method on staticmethod isn't going to help much in the long run. So I guess I'm -0 if classmethod gets a __call__ too. STeVe -- Grammar am for people who can't think for myself. --- Bucky Katt, Get Fuzzy ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
Steven Bethard wrote: My only (mild) concern is that if staticmethod is going to get a __call__, I think classmethod should probably get one too. Inside a class this doesn't make much sense: I agree, make sense or not, if @staticmethod def foo() and a simple def foo(self) can all be called inside class definition, @classmethod def foo(cls) should too. Anyway, my feeling was that running into this behavior (that staticmethod is not callable) is a good oportunity to explain how descriptors work. And once you start playing around with staticmethod and classmethod, you're going to need to learn that pretty soon anyway. Hiding it a little bit longer with a __call__ method on staticmethod isn't going to help much in the long run. The problem is that even after explaining descriptors (which IMHO can be a more advanced feature for newbies), you still need a workaround and you might end up with (or call __get__): class A: def foo(): pass bar = foo() foo = staticmethod(foo) Which I find sad considering all the energy debating @decorators;) Regards, Nicolas ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
On 3/1/06, Nicolas Fleury [EMAIL PROTECTED] wrote: The problem is that even after explaining descriptors (which IMHO can be a more advanced feature for newbies), you still need a workaround and you might end up with (or call __get__): class A: def foo(): pass bar = foo() foo = staticmethod(foo) Which I find sad considering all the energy debating @decorators;) In which context did you find a need for defining a static method and calling it inside the class definition? I'm guessing that what you're playing dubious scoping games. -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Making staticmethod objects callable?
Guido van Rossum wrote: In which context did you find a need for defining a static method and calling it inside the class definition? I'm guessing that what you're playing dubious scoping games. I'm not. I almost never use staticmethod actually. I find them not very pythonic, in my humble own definition of pythonic. But since staticmethod is a standard built-in, I considered valid the question of a programmer relatively new to Python (but obviously appreciating its dynamic nature) wondering why calling a static method inside a class definition doesn't work. A use case is not hard to imagine, especially a private static method called only to build a class attribute. I don't know the philosophy behind making staticmethod a built-in (instead of a function in a module only used in specific occasions), but my guess was that what is normal scoping/regrouping in Java/C++/C# was worth common use support in Python. But your comment about dubious scoping games makes me think I, again, didn't guess right;) So yes, I'm proposing something I'll probably never use, but I think would make Python more welcoming. Regards, Nicolas ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com