use object method without initializing object

R

Reckoner

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?
 
C

colas.francis

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
 
R

Robert Bossy

Reckoner said:
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
 
C

colas.francis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top