use object method without initializing object

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

    use object method without initializing object

    would it be possible to use one of an object's methods without
    initializing the object?

    In other words, if I have:

    class Test:
    def __init__(self):
    print 'init'
    def foo(self):
    print 'foo'

    and I want to use the foo function without hitting the
    initialize constructor function.

    Is this possible?
  • colas.francis@gmail.com

    #2
    Re: use object method without initializing object

    On 15 avr, 17:27, Reckoner <recko...@gmail .comwrote:
    would it be possible to use one of an object's methods without
    initializing the object?
    >
    In other words, if I have:
    >
    class Test:
    def __init__(self):
    print 'init'
    def foo(self):
    print 'foo'
    >
    and I want to use the foo function without hitting the
    initialize constructor function.
    >
    Is this possible?
    Yes:

    In [214]: class Test:
    .....: def foo(self):
    .....: print 'foo'
    .....:

    In [215]: t = Test()

    In [216]: t.foo()
    foo

    Comment

    • Robert Bossy

      #3
      Re: use object method without initializing object

      Reckoner wrote:
      would it be possible to use one of an object's methods without
      initializing the object?
      >
      In other words, if I have:
      >
      class Test:
      def __init__(self):
      print 'init'
      def foo(self):
      print 'foo'
      >
      and I want to use the foo function without hitting the
      initialize constructor function.
      >
      Is this possible?
      >
      Hi,

      Yes. It is possible and it is called "class method". That is to say, it
      is a method bound to the class, and not to the class instances.
      In pragmatic terms, class methods have three differences from instance
      methods:
      1) You have to declare a classmethod as a classmethod with the
      classmethod() function, or the @classmethod decorator.
      2) The first argument is not the instance but the class: to mark this
      clearly, it is usually named cls, instead of self.
      3) Classmethods are called with class objects, which looks like this:
      ClassName.class _method_name(.. .).

      In your example, this becomes:

      class Test(object):
      def __init__(self):
      print 'init'
      @classmethod
      def foo(cls):
      print 'foo'


      Now call foo without instantiating a Test:
      Test.foo()

      RB

      Comment

      • colas.francis@gmail.com

        #4
        Re: use object method without initializing object

        On 15 avr, 17:43, Robert Bossy <Robert.Bo...@j ouy.inra.frwrot e:
        Reckoner wrote:
        would it be possible to use one of an object's methods without
        initializing the object?
        >
        In other words, if I have:
        >
        class Test:
        def __init__(self):
        print 'init'
        def foo(self):
        print 'foo'
        >
        and I want to use the foo function without hitting the
        initialize constructor function.
        >
        Is this possible?
        >
        Hi,
        >
        Yes. It is possible and it is called "class method". That is to say, it
        is a method bound to the class, and not to the class instances.
        In pragmatic terms, class methods have three differences from instance
        methods:
        1) You have to declare a classmethod as a classmethod with the
        classmethod() function, or the @classmethod decorator.
        2) The first argument is not the instance but the class: to mark this
        clearly, it is usually named cls, instead of self.
        3) Classmethods are called with class objects, which looks like this:
        ClassName.class _method_name(.. .).
        >
        In your example, this becomes:
        >
        class Test(object):
        def __init__(self):
        print 'init'
        @classmethod
        def foo(cls):
        print 'foo'
        >
        Now call foo without instantiating a Test:
        Test.foo()
        To be complete, you can also define a static method that will not even
        be passed the class as argument:

        In [217]: class Test(object):
        .....: def __init__(self):
        .....: print 'init'
        .....: @staticmethod
        .....: def foo():
        .....: print 'foo'
        .....:

        In [218]: Test.foo()
        foo

        Comment

        Working...