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
66
votes
5 answers

How to understand the difference between class_eval() and instance_eval()?

Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=>…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
53
votes
7 answers

Is it bad form to call a classmethod as a method from an instance?

Ex. If I have something like this: class C(object): @classmethod def f(cls, x): return x + x This will work: c = C() c.f(2) 4 But is that bad form? Should I only call C.f() or c.__class__.f() Obviously, this would only make…
Josh Gibson
  • 21,808
  • 28
  • 67
  • 63
53
votes
2 answers

ActiveRecord Rails 3 scope vs class method

I'm new to the new query interface of ActiveRecord so I'm still figuring things out. I was hoping someone could explain the difference between using a scope in an ActiveRecord model and just using a class method (ie self.some_method) From what I…
brad
  • 31,987
  • 28
  • 102
  • 155
52
votes
6 answers

Can you use a string to instantiate a class?

I'm using a Builder pattern in Python to separate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named ID... (e.g. ID12345). These all inherit from the base Builder class. In my script, I need to…
scottm
  • 27,829
  • 22
  • 107
  • 159
41
votes
3 answers

Python - how can I get the class name from within a class method - using @classmethod

I have the following code: class ObjectOne(object): @classmethod def print_class_name(cls): print cls.__class__.__name__ def print_class_name_again(self): print self.__class__.__name__ if __name__ == '__main__': …
tadasajon
  • 14,276
  • 29
  • 92
  • 144
36
votes
3 answers

How to dynamically define a class method which will refer to a local variable outside?

class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end end I know, this is not correct, because the def created a new scope. I also know that use define_method can create a instance method…
Croplio
  • 3,433
  • 6
  • 31
  • 37
36
votes
3 answers

Swift Declare Class Func in Protocol

I had following confusion. As far as I know the main difference between static and class keywords when declaring method is that the second one could be overridden in subclasses. The problem However when I declare a protocol in Swift 1.2 like…
hris.to
  • 6,235
  • 3
  • 46
  • 55
35
votes
5 answers

What can a 'const' method change?

C++ methods allow a const qualifier to indicate that the object is not changed by the method. But what does that mean? Eg. if the instance variables are pointers, does it mean that the pointers are not changed, or also that the memory to which they…
Daniel
  • 924
  • 1
  • 11
  • 16
34
votes
3 answers

Why use classmethod instead of staticmethod?

I know what they do and I've seen many examples of both, but I haven't found a single example where I would have to use classmethod instead of replacing it with a staticmethod. The most common example of classmethod I've seen is for creating a new…
Patrik Lippojoki
  • 1,603
  • 4
  • 19
  • 22
34
votes
4 answers

How do I list all objects created from a class in Ruby?

Is there any way in Ruby for a class to know how many instances of it exist and can it list them? Here is a sample class: class Project attr_accessor :name, :tasks def initialize(options) @name = options[:name] @tasks =…
Amit Erandole
  • 11,995
  • 23
  • 65
  • 103
33
votes
5 answers

What's the difference between "class method" and "static method"?

I've worked with a few different languages such as Java, C#, and Objective-C. In most languages, methods that don't require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
33
votes
5 answers

Static classes in Python

I once read (I think on a page from Microsoft) that it's a good way to use static classes, when you don't NEED two or more instances of a class. I'm writing a program in Python. Is it a bad style, if I use @classmethod for every method of a class?
rynd
  • 1,865
  • 5
  • 22
  • 24
30
votes
6 answers

Same name for classmethod and instancemethod

I'd like to do something like this: class X: @classmethod def id(cls): return cls.__name__ def id(self): return self.__class__.__name__ And now call id() for either the class or an instance of it: >>> X.id() 'X' >>>…
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
30
votes
1 answer

Rails –Testing named scopes: test scope results or scope configuration?

How should Rails named scopes be tested? Do you test the results returned from a scope, or that your query is configured correctly? If I have a User class with an .admins method like: class User < ActiveRecord::Base def self.admins …
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
30
votes
1 answer

Factory method for objects - best practice?

This is a question regarding the best practice for creating an instance of a class or type from different forms of the same data using python. Is it better to use a class method or is it better to use a separate function altogether? Let's say I have…
Yani
  • 1,465
  • 2
  • 16
  • 25
1
2
3
60 61