Class Methods Vs Any Other Callable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vbgunz

    Class Methods Vs Any Other Callable

    I remember learning closures in Python and thought it was the dumbest
    idea ever. Why use a closure when Python is fully object oriented? I
    didn't grasp the power/reason for them until I started learning
    JavaScript and then BAM, I understood them.

    Just a little while ago, I had a fear of decorators because I really
    couldn't find a definitive source to learn them (how to with with @).
    How important are they? They must be important otherwise why have'em
    in the language? I had to learn'em and then suddenly, BAM. I
    understand them.

    My main issue with closures and decorators was hidden in the fact of
    how *dead simple* they were. All I needed were reasons to use them
    over doing it X style. So what is my point? How dead simple are class
    methods? I must be missing there point so I am convinced they must be
    dead simple.

    classes, functions, instance and static methods are easy. So easy in
    fact, I could shoot myself in the foots without looking (preferably
    without aiming). So, why am I stuck on the *idea* of a class method?

    An instance method works on the instance
    A Static method is basically a function nested within a class object
    A class method is overkill?

    I can call a static or class method through either the class OR any
    instance of it. I've never designed a method that took advantage of
    the class name except in cases where I needed to extend a super class
    *but* even in this case, I didn't use the enclosing class name...

    Whats the deal with class methods, why use them over anything else?
    What does a class method accomplish in at least one line shorter than
    anything else? Does it help reduce duplication or typing? I am at a
    lost for words that can shed at least *one* good reason to use them.

    What is the one greatest reason to use them? A little syntax and
    explanation can go a long long way. I am failing to understand them so
    any help is really appreciated here!
  • Diez B. Roggisch

    #2
    Re: Class Methods Vs Any Other Callable

    An instance method works on the instance
    A Static method is basically a function nested within a class object
    A class method is overkill?
    If anything, a static method is overkill. See it this way: *if* you for some
    reason put a method into an enclosing context - isn't it worth having a
    reference to that?
    I can call a static or class method through either the class OR any
    instance of it. I've never designed a method that took advantage of
    the class name except in cases where I needed to extend a super class
    *but* even in this case, I didn't use the enclosing class name...
    >
    Whats the deal with class methods, why use them over anything else?
    What does a class method accomplish in at least one line shorter than
    anything else? Does it help reduce duplication or typing? I am at a
    lost for words that can shed at least *one* good reason to use them.
    >
    What is the one greatest reason to use them? A little syntax and
    explanation can go a long long way. I am failing to understand them so
    any help is really appreciated here!
    There is not one greatest reason. They have their uses as e.g.
    factory-methods for instances of the class. Or as e.g. registering
    functions for notifications amongst instances of the class. In which case
    writing

    class Foo:

    @classmethod
    def register(cls, listener):
    cls.LISTENERS.a ppend(listener)

    is much more robust because you could rename Foo the way you like - register
    won't be affected.

    Diez




    Comment

    • George Sakkis

      #3
      Re: Class Methods Vs Any Other Callable

      On May 14, 10:19 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
      An instance method works on the instance
      A Static method is basically a function nested within a class object
      A class method is overkill?
      >
      If anything, a static method is overkill. See it this way: *if* you for some
      reason put a method into an enclosing context - isn't it worth having a
      reference to that?
      My feeling exactly; these days I almost always use class methods
      instead of static. I vaguely remember seeing somewhere an example
      where a static method was the only (elegant) solution; neither a class
      method nor a plain function would do. I'll post it if I find it unless
      someone beats me to it.

      George

      Comment

      • vbgunz

        #4
        Re: Class Methods Vs Any Other Callable

        An instance method works on the instance
        A Static method is basically a function nested within a class object
        A class method is overkill?
        >
        If anything, a static method is overkill...
        class Foo:
        >
           @classmethod
           def register(cls, listener):
               cls.LISTENERS.a ppend(listener)
        When I learned about static methods, I learned they're a way to
        tightly couple some functionality with a class without tying the
        functionality to any of the instances. I see them as nothing more than
        a design decision. To me they make some sense.

        Other than a methods signature (classmethod(cl s, l) and a
        staticmethod(l) ) a class method does anything that a static method
        does and gets the CLS reference for FREE? Is this why a static method
        is considered to be overkill? In other words, either one can be called
        from either the class or the instance and both work pretty much the
        same *but* only the class method includes the class for reference and
        the static method does not?

        The only real difference I see between an instance and either a class
        or static method is the whole bound/unbound thing. Otherwise, even an
        instance can do what the others do *just* the instance method can only
        make those calls through an instance and not the class.

        Instance methods make the most sense. A static method makes sense too
        *but* I can see how a class method not only does what a static method
        does but how a class method *also* gets the cls reference for free.

        Am I correct?

        Comment

        • Diez B. Roggisch

          #5
          Re: Class Methods Vs Any Other Callable

          When I learned about static methods, I learned they're a way to
          tightly couple some functionality with a class without tying the
          functionality to any of the instances. I see them as nothing more than
          a design decision. To me they make some sense.
          Which you can say exactly about classmethods - because there is *no*
          instance.
          Other than a methods signature (classmethod(cl s, l) and a
          staticmethod(l) ) a class method does anything that a static method
          does and gets the CLS reference for FREE?
          Yes.
          Is this why a static method
          is considered to be overkill? In other words, either one can be called
          from either the class or the instance and both work pretty much the
          same *but* only the class method includes the class for reference and
          the static method does not?
          Yes.
          The only real difference I see between an instance and either a class
          or static method is the whole bound/unbound thing. Otherwise, even an
          instance can do what the others do *just* the instance method can only
          make those calls through an instance and not the class.
          Which is a pretty heavy distinction.
          Instance methods make the most sense. A static method makes sense too
          *but* I can see how a class method not only does what a static method
          does but how a class method *also* gets the cls reference for free.
          I don't understand the last part - but I certainly agree on the "instance
          methods make the most sense". But *if* you want a hierarchy
          of "sensefulne ss",

          method classmethod staticmethod

          is the ordering to apply I'd say. Again: classmethods get *more* context
          than staticmethods *without* needing an instance. More is better :)

          Diez

          Comment

          • vbgunz

            #6
            Re: Class Methods Vs Any Other Callable

            Instance methods make the most sense. A static method makes sense too
            *but* I can see how a class method not only does what a static method
            does but how a class method *also* gets the cls reference for free.
            >
            I don't understand the last part - but I certainly agree on the "instance
            methods make the most sense". But *if* you want a hierarchy
            of "sensefulne ss",
            >
            method classmethod staticmethod
            OK, I am sold. If in case I find myself needing a static-method, I'll
            simply make it a class-method instead. One thing that stuck out from
            earlier and I'll compare it to linking is the "relative" usefulness of
            cls. There are 2 conditions I can immediately come up with. 1, rename
            the class and the class method still works. 2, move the method from
            working on one particular class and it'll most likely work unchanged
            (w/ the same interface) in another.

            Other than the 2 reasons above (2 making more sense), what is a really
            good reason to pull out the class method. In other words, when you see
            one, what is the first thing that comes to mind? When you write one,
            what was the first thing on your mind? Other than "similar to static-
            methods", at what point will you be glad you used one? To sum it up,
            what is the best role for a class-method that does not turn your work
            into code-soup?

            Comment

            • vbgunz

              #7
              Re: Class Methods Vs Any Other Callable

              Instance methods make the most sense. A static method makes sense too
              *but* I can see how a class method not only does what a static method
              does but how a class method *also* gets the cls reference for free.
              >
              I don't understand the last part - but I certainly agree on the "instance
              methods make the most sense". But *if* you want a hierarchy
              of "sensefulne ss",
              >
              method classmethod staticmethod
              Sorry I quoted this earlier and meant to respond to it. What I meant
              was, might as well use a class method over a static method AND in
              doing so, cls is free. I sort of repeated the same thing over and
              over. I think my main concern was trying to make sure I could grasp at
              least that much.

              Comment

              • Arnaud Delobelle

                #8
                Re: Class Methods Vs Any Other Callable

                George Sakkis <george.sakkis@ gmail.comwrites :
                On May 14, 10:19 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                >
                An instance method works on the instance
                A Static method is basically a function nested within a class object
                A class method is overkill?
                >>
                >If anything, a static method is overkill. See it this way: *if* you for some
                >reason put a method into an enclosing context - isn't it worth having a
                >reference to that?
                >
                My feeling exactly; these days I almost always use class methods
                instead of static. I vaguely remember seeing somewhere an example
                where a static method was the only (elegant) solution; neither a class
                method nor a plain function would do. I'll post it if I find it unless
                someone beats me to it.
                >
                George
                __new__ is a static method!

                --
                Arnaud

                Comment

                • bruno.desthuilliers@gmail.com

                  #9
                  Re: Class Methods Vs Any Other Callable

                  On 14 mai, 19:45, Arnaud Delobelle <arno...@google mail.comwrote:
                  George Sakkis <george.sak...@ gmail.comwrites :
                  On May 14, 10:19 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                  >
                  An instance method works on the instance
                  A Static method is basically a function nested within a class object
                  A class method is overkill?
                  >
                  If anything, a static method is overkill. See it this way: *if* you for some
                  reason put a method into an enclosing context - isn't it worth having a
                  reference to that?
                  >
                  My feeling exactly; these days I almost always use class methods
                  instead of static. I vaguely remember seeing somewhere an example
                  where a static method was the only (elegant) solution; neither a class
                  method nor a plain function would do. I'll post it if I find it unless
                  someone beats me to it.
                  >
                  George
                  >
                  __new__ is a static method!
                  __new__ is a special-cased staticmethod that 1/ must not be declared
                  as such and 2/ takes the class object as first args. As far as I'm
                  concerned, it's semantically a classmethod.

                  Comment

                  • bruno.desthuilliers@gmail.com

                    #10
                    Re: Class Methods Vs Any Other Callable

                    On 14 mai, 16:30, George Sakkis <george.sak...@ gmail.comwrote:
                    On May 14, 10:19 am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    >
                    An instance method works on the instance
                    A Static method is basically a function nested within a class object
                    A class method is overkill?
                    >
                    If anything, a static method is overkill. See it this way: *if* you for some
                    reason put a method into an enclosing context - isn't it worth having a
                    reference to that?
                    >
                    My feeling exactly; these days I almost always use class methods
                    instead of static. I vaguely remember seeing somewhere an example
                    where a static method was the only (elegant) solution; neither a class
                    method nor a plain function would do. I'll post it if I find it unless
                    someone beats me to it.
                    No concrete example here but I surely remember having used
                    staticmethods in one case where I needed class-based polymorphic
                    dispatch and eventually implementation inheritance (to get a default
                    implementation) for something that didn't required access to the
                    class object.


                    George

                    Comment

                    • Arnaud Delobelle

                      #11
                      Re: Class Methods Vs Any Other Callable

                      "bruno.desthuil liers@gmail.com " <bruno.desthuil liers@gmail.com writes:
                      On 14 mai, 19:45, Arnaud Delobelle <arno...@google mail.comwrote:
                      >__new__ is a static method!
                      >
                      __new__ is a special-cased staticmethod that 1/ must not be declared
                      as such and 2/ takes the class object as first args. As far as I'm
                      concerned, it's semantically a classmethod.
                      It's a static method!

                      --
                      Arnaud

                      Comment

                      • Carl Banks

                        #12
                        Re: Class Methods Vs Any Other Callable

                        On May 14, 12:21 pm, vbgunz <vbg...@gmail.c omwrote:
                        Other than the 2 reasons above (2 making more sense), what is a really
                        good reason to pull out the class method. In other words, when you see
                        one, what is the first thing that comes to mind? When you write one,
                        what was the first thing on your mind? Other than "similar to static-
                        methods", at what point will you be glad you used one? To sum it up,
                        what is the best role for a class-method that does not turn your work
                        into code-soup?

                        When classmethods are inherited, they are called with the class it was
                        invoked with, rather than the class they were defined in. Try this
                        example:


                        class A(object):
                        @classmethod
                        def classname(cls):
                        print cls.__name__

                        @staticmethod
                        def staticname():
                        print "A"

                        class B(A):
                        pass

                        B.staticname()
                        B.classname()


                        Carl Banks

                        Comment

                        • bruno.desthuilliers@gmail.com

                          #13
                          Re: Class Methods Vs Any Other Callable

                          On 14 mai, 22:44, Arnaud Delobelle <arno...@google mail.comwrote:
                          "bruno.desthuil li...@gmail.com " <bruno.desthuil li...@gmail.com writes:
                          On 14 mai, 19:45, Arnaud Delobelle <arno...@google mail.comwrote:
                          __new__ is a static method!
                          >
                          __new__ is a special-cased staticmethod that 1/ must not be declared
                          as such and 2/ takes the class object as first args. As far as I'm
                          concerned, it's semantically a classmethod.
                          >
                          It's a static method!
                          Sorry Arnaud, I probably didn't made it clear enough : I do know it is
                          a staticmethod object. What I say is that
                          1/ it's special-cased since you don't *explicitely* declare it as a
                          staticmethod
                          2/ it behaves just like a classmethod, since it takes the class as
                          first argument.

                          IOW, I'm talking about semantic, not implementation.
                          --
                          Arnaud

                          Comment

                          • Ivan Illarionov

                            #14
                            Re: Class Methods Vs Any Other Callable

                            On Wed, 14 May 2008 09:21:10 -0700, vbgunz wrote:
                            [...]
                            when you see
                            one, what is the first thing that comes to mind? When you write one,
                            what was the first thing on your mind? Other than "similar to static-
                            methods", at what point will you be glad you used one? To sum it up,
                            what is the best role for a class-method that does not turn your work
                            into code-soup?
                            The first thing on my mind when I see a classmethod is that I may need a
                            metaclass here.

                            class A(object):
                            @classmethod
                            def do_something_wi th_aclass(cls):
                            pass

                            @classmethod
                            def do_something_di ffferent_with_a class(cls):
                            pass

                            def do_something(se lf):
                            pass

                            is similar to:

                            class M(type):
                            def do_something_wi th_aclass(cls):
                            pass

                            def do_something_di ffferent_with_a class(cls):
                            pass

                            class A:
                            __metaclass__ = M

                            def do_something(se lf):
                            pass

                            -- Ivan

                            Comment

                            • Arnaud Delobelle

                              #15
                              Re: Class Methods Vs Any Other Callable

                              "bruno.desthuil liers@gmail.com " <bruno.desthuil liers@gmail.com writes:
                              On 14 mai, 22:44, Arnaud Delobelle <arno...@google mail.comwrote:
                              >"bruno.desthui lli...@gmail.co m" <bruno.desthuil li...@gmail.com writes:
                              On 14 mai, 19:45, Arnaud Delobelle <arno...@google mail.comwrote:
                              >__new__ is a static method!
                              >>
                              __new__ is a special-cased staticmethod that 1/ must not be declared
                              as such and 2/ takes the class object as first args. As far as I'm
                              concerned, it's semantically a classmethod.
                              >>
                              >It's a static method!
                              >
                              Sorry Arnaud, I probably didn't made it clear enough : I do know it is
                              a staticmethod object. What I say is that
                              1/ it's special-cased since you don't *explicitely* declare it as a
                              staticmethod
                              2/ it behaves just like a classmethod, since it takes the class as
                              first argument.
                              >
                              IOW, I'm talking about semantic, not implementation.
                              .... and I was being facetious, not serious :)

                              --
                              Arnaud

                              Comment

                              Working...