Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > calling one staticmethod from another

Reply
Thread Tools

calling one staticmethod from another

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      10-30-2012
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
 
Reply With Quote
 
 
 
 
Ethan Furman
Guest
Posts: n/a
 
      10-30-2012
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~
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      10-30-2012
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

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      10-30-2012
On Tue, Oct 30, 2012 at 7:41 AM, Ethan Furman <> 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.
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      10-30-2012
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

 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      10-30-2012
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-i...on-either.html

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: calling one staticmethod from another Jean-Michel Pichavant Python 1 10-31-2012 07:59 AM
staticmethod vs metaclass Robin Becker Python 0 08-11-2004 05:27 PM
Re: __getitem__ and classmethod/staticmethod Andrew Bennetts Python 0 06-25-2004 06:00 PM
__getitem__ and classmethod/staticmethod Karl Chen Python 0 06-25-2004 05:18 PM
error: 'staticmethod' object is not callable Michal Vitecek Python 4 02-11-2004 03:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57