class method vs static method

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

    #1

    class method vs static method

    Hi,

    I've done some reading up but I cant get my head around how/why class
    methods can be used as apposed to static methods.

    Can anyone give an example of where they have used class methods?

    Thanks,
    Chris

  • Marc 'BlackJack' Rintsch

    #2
    Re: class method vs static method

    In <1164098781.577 358.90850@j44g2 000cwa.googlegr oups.com>, Chris Brat
    wrote:
    I've done some reading up but I cant get my head around how/why class
    methods can be used as apposed to static methods.
    >
    Can anyone give an example of where they have used class methods?
    ----
    class Spam(object):
    def __init__(self, data):
    self.data = data

    @classmethod
    def load(cls, filename):
    # Open file and load data.
    data = 'eric'
    return cls(data)

    class Ham(Spam):
    pass

    ham = Ham.load('test. dat')
    print type(ham)
    ----

    Here the type of `ham` is ``<class '__main__.Ham'> `` because a class
    method will receive the class on which the method is called as first
    argument. In a static method you don't know if it is called on the class
    where the method is defined or on a subclass.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Michele Simionato

      #3
      Re: class method vs static method


      Chris Brat wrote:
      Hi,
      >
      I've done some reading up but I cant get my head around how/why class
      methods can be used as apposed to static methods.
      >
      Can anyone give an example of where they have used class methods?
      >
      Thanks,
      Chris
      Class methods are typically used as factories:

      class MyClass(object) :
      @classmethod
      def myfactory(cls):
      self = cls()
      initialize(self )
      return self

      With different factories you can obtain instances of the same classes
      initialized in different ways. This is probably better than writing a
      lot of subclasses
      with different __init__s. This is my typical use case, but I am sure
      others will post
      many other examples of application.

      Michele Simionato

      Comment

      • Bruno Desthuilliers

        #4
        Re: class method vs static method

        Chris Brat wrote:
        Hi,
        >
        I've done some reading up but I cant get my head around how/why class
        methods can be used as apposed to static methods.
        a "static method" is actually not much more than a plain function
        living in a class's namespace. classmethods, OTOH, have access to the
        class object.
        Can anyone give an example of where they have used class methods?
        The canonical use case is factory methods, but you'll typically find
        other possible applications in frameworks.

        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"

        Comment

        • Chris Brat

          #5
          Re: class method vs static method

          Thanks for the responses - things are clearer now.

          Comment

          Working...