473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

class method vs static method

Hi,

I've done some reading up but I cant get my head around how/why class
methods can be used as apposed to static methods.

Can anyone give an example of where they have used class methods?

Thanks,
Chris

Nov 21 '06 #1
4 2426
In <11*********************@j44g2000cwa.googlegroups. com>, Chris Brat
wrote:
I've done some reading up but I cant get my head around how/why class
methods can be used as apposed to static methods.

Can anyone give an example of where they have used class methods?
----
class Spam(object):
def __init__(self, data):
self.data = data

@classmethod
def load(cls, filename):
# Open file and load data.
data = 'eric'
return cls(data)

class Ham(Spam):
pass

ham = Ham.load('test.dat')
print type(ham)
----

Here the type of `ham` is ``<class '__main__.Ham'>`` because a class
method will receive the class on which the method is called as first
argument. In a static method you don't know if it is called on the class
where the method is defined or on a subclass.

Ciao,
Marc 'BlackJack' Rintsch
Nov 21 '06 #2

Chris Brat wrote:
Hi,

I've done some reading up but I cant get my head around how/why class
methods can be used as apposed to static methods.

Can anyone give an example of where they have used class methods?

Thanks,
Chris
Class methods are typically used as factories:

class MyClass(object):
@classmethod
def myfactory(cls):
self = cls()
initialize(self)
return self

With different factories you can obtain instances of the same classes
initialized in different ways. This is probably better than writing a
lot of subclasses
with different __init__s. This is my typical use case, but I am sure
others will post
many other examples of application.

Michele Simionato

Nov 21 '06 #3
Chris Brat wrote:
Hi,

I've done some reading up but I cant get my head around how/why class
methods can be used as apposed to static methods.
a "static method" is actually not much more than a plain function
living in a class's namespace. classmethods, OTOH, have access to the
class object.
Can anyone give an example of where they have used class methods?
The canonical use case is factory methods, but you'll typically find
other possible applications in frameworks.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 21 '06 #4
Thanks for the responses - things are clearer now.

Nov 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
6
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.