Class Methods Vs Any Other Callable

Carl Banks pavlovevidence at gmail.com
Wed May 14 17:47:12 EDT 2008


On May 14, 12:21 pm, vbgunz <vbg... at gmail.com> wrote:
> 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



More information about the Python-list mailing list