use object method without initializing object

Discussion in 'Python' started by Reckoner, Apr 15, 2008.

  1. Reckoner

    Reckoner Guest

    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?
     
    Reckoner, Apr 15, 2008
    #1
    1. Advertising

  2. Reckoner

    Guest

    On 15 avr, 17:27, 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?


    Yes:

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

    In [215]: t = Test()

    In [216]: t.foo()
    foo
     
    , Apr 15, 2008
    #2
    1. Advertising

  3. Reckoner

    Robert Bossy Guest

    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
     
    Robert Bossy, Apr 15, 2008
    #3
  4. Reckoner

    Guest

    On 15 avr, 17:43, Robert Bossy <> wrote:
    > 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
     
    , Apr 15, 2008
    #4
    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. Calle Pettersson

    Initializing without assigning

    Calle Pettersson, Jul 19, 2006, in forum: C++
    Replies:
    17
    Views:
    659
    Frederick Gotham
    Jul 20, 2006
  2. Replies:
    10
    Views:
    494
    Zeppe
    Apr 12, 2007
  3. Pavan
    Replies:
    9
    Views:
    2,603
    James Kanze
    Mar 4, 2008
  4. ssylee
    Replies:
    12
    Views:
    603
    Richard Bos
    Feb 9, 2009
  5. Bala Paranj
    Replies:
    5
    Views:
    109
    Bala Paranj
    Jun 21, 2007
Loading...

Share This Page