473,785 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2444
In <11************ *********@j44g2 000cwa.googlegr oups.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
2441
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
19
105841
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: System.out.print((char)27 + "[2J");
6
3639
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 purpose. This works fine if I already have an instantiation of the class, but not when I try this on the class object directly. A bare bones example:
5
14439
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 the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
9
5849
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.