Questions tagged [class-method]

Methods that are called on a class instead of on an object.

Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language: In some languages (e.g. C++, Java), class methods are synonymous with static methods (see section below), which are called with a known class name at compile-time. this cannot be used in static methods.

In some other languages (e.g. Smalltalk, Ruby, Objective-C), class methods are methods that are called on a class object, which can be computed at runtime, there being no difference between calling a method on a regular object or a class object; thus both instance and class methods are resolved dynamically, and there are no "static" methods. Notably, in these class methods, this refers to the class object.

Some languages have both. For example, in Python, one can create class methods and static methods using the classmethod and staticmethod decorators, respectively. The former has access to this (i.e. the instance object, conventionally known as self), while the latter does not.

904 questions
4574
votes
36 answers

@staticmethod vs @classmethod in Python

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
1886
votes
12 answers

Meaning of @classmethod and @staticmethod for beginner

What do @classmethod and @staticmethod mean in Python, and how are they different? When should I use them, why should I use them, and how should I use them? As far as I understand, @classmethod tells a class that it's a method which should be…
user1632861
463
votes
18 answers

What is the difference between class and instance methods?

What's the difference between a class method and an instance method? Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?
Devoted
  • 177,705
  • 43
  • 90
  • 110
401
votes
9 answers

Ruby: Calling class method from instance

In Ruby, how do you call a class method from one of that class's instances? Say I have class Truck def self.default_make # Class method. "mac" end def initialize # Instance method. Truck.default_make # gets the default via…
Peter
  • 127,331
  • 53
  • 180
  • 211
291
votes
18 answers

What is the purpose of class methods?

I'm teaching myself Python and my most recent lesson was that Python is not Java, and so I've just spent a while turning all my Class methods into functions. I now realise that I don't need to use Class methods for what I would done with static…
David Webb
  • 190,537
  • 57
  • 313
  • 299
214
votes
9 answers

How to make a class property?

In python I can add a method to a class with the @classmethod decorator. Is there a similar decorator to add a property to a class? I can better show what I'm talking about. class Example(object): the_I = 10 def __init__( self ): …
deft_code
  • 57,255
  • 29
  • 141
  • 224
153
votes
11 answers

Calling C++ member functions via a function pointer

How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write: class Dog : Animal { Dog (); void bark (); } … Dog* pDog = new Dog (); BarkFunction pBark =…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
133
votes
3 answers

When should I use @classmethod and when def method(self)?

While integrating a Django app I have not used before, I found two different ways to define functions inside the class. The author seems to use them both distinctively and intentionally. The first one is the one that I myself use a lot: class…
marue
  • 5,588
  • 7
  • 37
  • 65
130
votes
4 answers

Calling a base class's classmethod in Python

Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class Derived(Base): @classmethod def do(cls, a): print 'In derived!' # Base.do(cls, a) -- can't pass `cls` …
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
116
votes
6 answers

How do I use define_method to create class methods?

This is useful if you are trying to create class methods metaprogramatically: def self.create_methods(method_name) # To create instance methods: define_method method_name do ... end # To create class methods that refer to the…
Chinasaur
  • 2,108
  • 2
  • 16
  • 17
101
votes
5 answers

Using super with a class method

I'm trying to learn the super() function in Python. I thought I had a grasp of it until I came over this example (2.6) and found myself…
dza
  • 1,478
  • 2
  • 13
  • 24
83
votes
7 answers

What's an example use case for a Python classmethod?

I've read What are Class methods in Python for? but the examples in that post are complex. I am looking for a clear, simple, bare-bones example of a particular use case for classmethods in Python. Can you name a small, specific example use case…
coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
81
votes
3 answers

Should constructors comply with the Liskov Substitution Principle?

I usually try to make sure my object instances comply with the Liskov Substitution Principle, but I've always wondered is do people think LSP should apply to constructors too? I've tried googling for this but I haven't been able to find any strong…
dkubb
  • 2,126
  • 18
  • 16
78
votes
5 answers

__getattr__ for static/class variables

I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python? I know it is possible if I create an instance…
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
77
votes
11 answers

Attaching a decorator to all functions within a class

Is there a way to bind a decorator to all functions within a class generically, rather than explicitly stating it for every function? I suppose it then becomes a kind of aspect, rather than a decorator and it does feel a bit odd, but was thinking…
dochead
  • 1,785
  • 2
  • 18
  • 20
1
2 3
60 61