Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32486 > unrolled thread

calling one staticmethod from another

Started byUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
First post2012-10-30 13:25 +0100
Last post2012-10-30 21:29 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  calling one staticmethod from another Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-30 13:25 +0100
    Re: calling one staticmethod from another Ethan Furman <ethan@stoneleaf.us> - 2012-10-30 06:41 -0700
    Re: calling one staticmethod from another Dave Angel <d@davea.name> - 2012-10-30 09:47 -0400
      Re: calling one staticmethod from another Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-30 16:39 +0100
    Re: calling one staticmethod from another Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-30 09:27 -0600
    Re: calling one staticmethod from another Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-30 21:29 +0000

#32486 — calling one staticmethod from another

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-10-30 13:25 +0100
Subjectcalling one staticmethod from another
Message-ID<tit4m9-1ei.ln1@satorlaser.homedns.org>
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

[toc] | [next] | [standalone]


#32489

FromEthan Furman <ethan@stoneleaf.us>
Date2012-10-30 06:41 -0700
Message-ID<mailman.3090.1351604780.27098.python-list@python.org>
In reply to#32486
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~

[toc] | [prev] | [next] | [standalone]


#32490

FromDave Angel <d@davea.name>
Date2012-10-30 09:47 -0400
Message-ID<mailman.3091.1351604905.27098.python-list@python.org>
In reply to#32486
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

[toc] | [prev] | [next] | [standalone]


#32498

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-10-30 16:39 +0100
Message-ID<6v85m9-17j.ln1@satorlaser.homedns.org>
In reply to#32490
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

[toc] | [prev] | [next] | [standalone]


#32497

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-10-30 09:27 -0600
Message-ID<mailman.3095.1351610873.27098.python-list@python.org>
In reply to#32486
On Tue, Oct 30, 2012 at 7:41 AM, Ethan Furman <ethan@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.

[toc] | [prev] | [next] | [standalone]


#32504

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-10-30 21:29 +0000
Message-ID<mailman.3103.1351632558.27098.python-list@python.org>
In reply to#32486
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web