Re: calling one staticmethod from another

2012-10-31 Thread Ulrich Eckhardt

Am 30.10.2012 18:23, schrieb Jean-Michel Pichavant:

- Original Message -
[snip]

I haven't figured out the justification for staticmethod,


http://en.wikipedia.org/wiki/Namespace
+
Namespaces are one honking great idea -- let's do more of those!

Someone may successfully use only modules as namespaces, but classes
can be used as well. It's up to you.


Indeed, see e.g. Steven D'Aprano's approach at formalizing that:

http://code.activestate.com/recipes/578279/


Greetings!

Uli

--
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Ethan Furman

Ulrich Eckhardt wrote:
I can call a staticmethod f() of class C like C.f() or with an 
instance like C().f(). Inside that staticmethod, I have neither the 
class (at least not the original one) nor do I have an instance, so I 
can't call a different staticmethod from the same class. The obvious 
solution is to make this a classmethod instead, with a mostly-unused 
cls parameter.


Am I missing something?


class Spam():
@staticmethod
def green():
print('on a train!')
@staticmethod
def question():
print('would you, could you', end='')
Spam.green()

It can be a pain if you change the class name, but it is certainly one way to 
do it.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Dave Angel
On 10/30/2012 08:25 AM, Ulrich Eckhardt wrote:
 Hi!

 I can call a staticmethod f() of class C like C.f() or with an
 instance like C().f(). Inside that staticmethod, I have neither the
 class (at least not the original one) nor do I have an instance, so I
 can't call a different staticmethod from the same class. The obvious
 solution is to make this a classmethod instead, with a mostly-unused
 cls parameter.

 Am I missing something?

 Uli

I'd think the obvious solution is to move both the functions outside of
the class.  I haven't figured out the justification for staticmethod,
except for java or C++ converts.

But if you like the staticmethod for other reasons, why is it you can't
just use
  C.g()

?

-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Ian Kelly
On Tue, Oct 30, 2012 at 7:41 AM, Ethan Furman et...@stoneleaf.us wrote:
 class Spam():
 @staticmethod
 def green():
 print('on a train!')
 @staticmethod
 def question():
 print('would you, could you', end='')
 Spam.green()

 It can be a pain if you change the class name, but it is certainly one way
 to do it.

It fails if the staticmethod being called has been overridden in a
subclass, though.  I think the only correct way to do it with
inheritance is by replacing it with a classmethod, as the OP
suggested.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Ulrich Eckhardt

Am 30.10.2012 14:47, schrieb Dave Angel:

I'd think the obvious solution is to move both the functions outside of
the class.  I haven't figured out the justification for staticmethod,
except for java or C++ converts.


Although I come from a C++ background, I think static functions have 
solid reasons that are not just based on my habits. When I see a static 
function in C++, I know that it is a function, not a method, so the only 
context it could interact with is also static (inside a namespace, 
including the global namespace or statically inside the class) or passed 
as parameters. Further, the function itself is inside a class (possibly 
even private), so it should only be of interest in the context of that 
class or instances thereof and doesn't collide with other functions.


In summary, putting utility code into a function reduces the context it 
interacts with. Putting that utility function as staticmethod inside a 
class further reduces the context of that function. Together, this also 
reduces the complexity of the code, making it easier to write and read.




But if you like the staticmethod for other reasons, why is it you can't
just use
   C.g()
?


This works. It's just that I find it a bit inconvenient/ugly to repeat 
the classname inside a class. But hey, coming from C++ I have gotten 
used to always writing self. to call one member function from another, 
so I'll probably survive this one, too. ;)



Greetings!

Uli

--
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Jean-Michel Pichavant


- Original Message -
[snip]
 I haven't figured out the justification for staticmethod,

http://en.wikipedia.org/wiki/Namespace
+
Namespaces are one honking great idea -- let's do more of those!

Someone may successfully use only modules as namespaces, but classes can be 
used as well. It's up to you.

Cheers,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling one staticmethod from another

2012-10-30 Thread Mark Lawrence

On 30/10/2012 12:25, Ulrich Eckhardt wrote:

Hi!

I can call a staticmethod f() of class C like C.f() or with an
instance like C().f(). Inside that staticmethod, I have neither the
class (at least not the original one) nor do I have an instance, so I
can't call a different staticmethod from the same class. The obvious
solution is to make this a classmethod instead, with a mostly-unused
cls parameter.

Am I missing something?

Uli


I hope that you find these useful.

http://dirtsimple.org/2004/12/python-is-not-java.html
http://dirtsimple.org/2004/12/java-is-not-python-either.html

--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list