470,849 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

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?
Jun 27 '08 #1
3 1705
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

Jun 27 '08 #2
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
Jun 27 '08 #3
On 15 avr, 17:43, Robert Bossy <Robert.Bo...@jouy.inra.frwrote:
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

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by t scytale | last post: by
13 posts views Thread by WaterWalk | last post: by
23 posts views Thread by tonytech08 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.