Class Methods help

B

bdsatish

Hi,

I have a question regarding the difference b/w "class methods" and
"object methods". Consider for example:

class MyClass:
x = 10

Now I can access MyClass.x -- I want a similar thing for functions. I
tried

class MyClass:
def some_func(x):
return x+2

When I call MyClass.some_func(10) -- it fails, with error message:


TypeError: unbound method some_func() must be called with MyClass
instance as first argument (got int instance instead)

OK. I figured out that something like this works:
obj = MyClass()
y = obj.some_func(10)

BUT, this means that we have functions applying for instances. That is
we have "instance method". Now, how do I implement some function which
I can invoke with the class name itself ? Instead of creating a dummy
object & then calling.... In short, how exactly do I create "class
methods" ??
 
T

Tim Chase

class MyClass:
def some_func(x):
return x+2

When I call MyClass.some_func(10) -- it fails, with error message:


TypeError: unbound method some_func() must be called with MyClass
instance as first argument (got int instance instead)

OK. I figured out that something like this works:
obj = MyClass()
y = obj.some_func(10)

BUT, this means that we have functions applying for instances. That is
we have "instance method". Now, how do I implement some function which
I can invoke with the class name itself ? Instead of creating a dummy
object & then calling.... In short, how exactly do I create "class
methods" ??

You mean the classmethod decorator? :)

class MyClass:
@classmethod
def some_func(cls, x):
return x+2

MyClass.some_func(42)

(the decorator syntax was added in 2.4, so if you need it in
pre-2.4, you'd have to do

class MyClass:
def some_func(cls, x):
return x+2
some_func = classmethod(some_func)

to get the same behavior)

-tkc
 
L

Lie Ryan

bdsatish said:
Hi,

I have a question regarding the difference b/w "class methods" and
"object methods". Consider for example:

class MyClass:
x = 10

Now I can access MyClass.x -- I want a similar thing for functions. I
tried

class MyClass:
def some_func(x):
return x+2

When I call MyClass.some_func(10) -- it fails, with error message:


TypeError: unbound method some_func() must be called with MyClass
instance as first argument (got int instance instead)

OK. I figured out that something like this works:
obj = MyClass()
y = obj.some_func(10)

BUT, this means that we have functions applying for instances. That is
we have "instance method". Now, how do I implement some function which
I can invoke with the class name itself ? Instead of creating a dummy
object & then calling.... In short, how exactly do I create "class
methods" ??
with staticmethod decorator:
.... @staticmethod
.... def some_func(x):
.... return x+2
....12
 
B

bd satish

Thanks to Tim Chase & Lie Ryan !! That was exactly what I was looking for !!

It's time for me to now read the documentation of "decorators" and
@classmethod and also @staticmethod.

I'm quite new to decorators...


-- Satish BD
 
J

Jean-Michel Pichavant

FYI, same without decorators, if you python version does not support it.

class MyClass:
def some_func(x):
return x+2
some_func = staticmethod(some_func)


JM
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top