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
29
votes
2 answers

Why always add self as first argument to class methods?

Possible Duplicate: Why do you need explicitly have the “self” argument into a Python method? I understand why self is always the first argument for class methods, this makes total sense, but if it's always the case, then why go through the…
jonathan topf
  • 7,897
  • 17
  • 55
  • 85
28
votes
4 answers

Cannot call a class method with [self theMethod:]

I am trying to write a class method in Objective C. The project builds fine when I declare the method. But the build fails whenever I try to call the method. Here is my code. Header File #import @interface LoginViewController :…
Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82
28
votes
2 answers

What is the difference between class method vs. class field function vs. class field arrow function?

What is the difference between class method, class property which is a function, and class property which is an arrow function? Does the this keyword behave differently in the different variants of the method? class Greeter { constructor() { …
Combine
  • 3,894
  • 2
  • 27
  • 30
27
votes
1 answer

super and __new__ confusion

As what I just learned, I can use super() this way: super(class, obj_of_class-or-_subclass_of_class) Code goes below: #Case 1 class A(object): def __init__(self): print "A init" class B(A): def __init__(self): print "B…
Alcott
  • 17,905
  • 32
  • 116
  • 173
27
votes
4 answers

Call a class method from within that class

Is there a way to call a class method from another method within the same class? For example: +classMethodA{ } +classMethodB{ //I would like to call classMethodA here }
prostock
  • 9,327
  • 19
  • 70
  • 118
26
votes
2 answers

Can a python @classmethod be inherited?

For example, I have a base class and a derived class: >>> class Base: ... @classmethod ... def myClassMethod(klass): ... pass ... >>> class Derived: ... pass ... >>> Base.myClassMethod() >>> Derived.myClassMethod() Traceback (most recent…
yuklai
  • 1,595
  • 1
  • 14
  • 26
25
votes
2 answers

Why is @staticmethod not preserved across classes, when @classmethod is?

Take the following example script: class A(object): @classmethod def one(cls): print("I am class") @staticmethod def two(): print("I am static") class B(object): one = A.one two =…
25
votes
6 answers

Various errors in code that tries to call classmethods

I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1): #---Do Something--- # A 'function map' that has function name as its keys and the above…
user1126425
  • 2,385
  • 4
  • 18
  • 17
24
votes
4 answers

What does 'self' refer to in a @classmethod?

I thought I was starting to get a grip on "the Python way" of programming. Methods of a class accept self as the first parameter to refer to the instance of the class whose context the method is being called in. The @classmethod decorator refers to…
Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
24
votes
1 answer

Calling class methods via class name vs self

Suppose we have a class named Calculator. There's a class method in it, called runProgram. If I wanted to call this class method, inside the class's implementation, what would the difference between these two be: [Calculator runProgram] OR [self…
Milad
  • 1,239
  • 3
  • 19
  • 37
23
votes
4 answers

Override method with different argument types in extended class - Typescript

I want to override a method and pass different argument types to it: class Base { public myMethod(myString: string): undefined { return; } } class Child extends Base { public myMethod(myNumber: number): undefined { …
A_A
  • 1,832
  • 2
  • 11
  • 17
22
votes
2 answers

Python - can I programmatically decorate class methods from a class instance?

I have an object hierarchy in which almost all of the methods are class methods. It looks like the following: class ParentObject(object): def __init__(self): pass @classmethod def smile_warmly(cls, the_method): def…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
21
votes
2 answers

why __getitem__ cannot be classmethod?

Suppose following class: class Class(object): @classmethod def getitem(*args): print 'getitem %s' % (args,) @classmethod def __getitem__(*args): print '__getitem__ %s' % (args,) The getitem method behaves as…
QwiglyDee
  • 789
  • 8
  • 18
20
votes
4 answers

Class methods which create new instances

Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"] According to widespread memory management guides (including Apple's), you're only responsible…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
20
votes
5 answers

Check if a function uses @classmethod

TL;DR How do I find out whether a function was defined using @classmethod or something with the same effect? My problem For implementing a class decorator I would like to check if a method takes the class as its first argument, for example as…
hlt
  • 6,219
  • 3
  • 23
  • 43
1 2
3
60 61