calling one staticmethod from another

Discussion in 'Python' started by Ulrich Eckhardt, Oct 30, 2012.

  1. 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
     
    Ulrich Eckhardt, Oct 30, 2012
    #1
    1. Advertising

  2. Ulrich Eckhardt

    Ethan Furman Guest

    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~
     
    Ethan Furman, Oct 30, 2012
    #2
    1. Advertising

  3. Ulrich Eckhardt

    Dave Angel Guest

    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
     
    Dave Angel, Oct 30, 2012
    #3
  4. Ulrich Eckhardt

    Ian Kelly Guest

    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.
     
    Ian Kelly, Oct 30, 2012
    #4
  5. 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
     
    Ulrich Eckhardt, Oct 30, 2012
    #5
  6. 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.
     
    Mark Lawrence, Oct 30, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    922
    loveNUNO
    Nov 20, 2003
  2. Michal Vitecek
    Replies:
    4
    Views:
    949
    Michael Hudson
    Feb 11, 2004
  3. Andrew Bennetts

    Re: __getitem__ and classmethod/staticmethod

    Andrew Bennetts, Jun 25, 2004, in forum: Python
    Replies:
    0
    Views:
    483
    Andrew Bennetts
    Jun 25, 2004
  4. Robert Cohen
    Replies:
    3
    Views:
    275
    Andrew Durstewitz
    Jul 15, 2003
  5. Jean-Michel Pichavant

    Re: calling one staticmethod from another

    Jean-Michel Pichavant, Oct 30, 2012, in forum: Python
    Replies:
    1
    Views:
    113
    Ulrich Eckhardt
    Oct 31, 2012
Loading...

Share This Page