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
-1
votes
1 answer

Editing Class methods def self.xxx

If I am editing the Array class, shouldn't I have to define each method with a self (e.g. self.sum). I'm not sure why this passes the rpsec tests for the 'Test-First' Ruby track without the self.method immediately following the def. class Array …
mdp299
  • 39
  • 1
  • 1
  • 5
-1
votes
1 answer

Confused: nested "for" loop and

The object of this program is to create a account program to indicate 2 questions per month. I recorded the month input, now the question is.. How do I write the loop to repeatedly ask the two questions and stop at the month the input number was set…
Relogical
  • 39
  • 1
  • 5
-1
votes
2 answers

Using methods from 2nd-level extended class

I'm trying to use methods from that is extended by the class that this class is extending from. An example of what I'm trying to do: class A def foo "Foobar" end end class B extend A end class C extend B end B.foo #=> "Foobar" C.foo…
Ethan Turkeltaub
  • 2,931
  • 8
  • 30
  • 45
-1
votes
2 answers

Objective C delegates and class methods vs instance methods

There is a similar question to mine on the following link but it doesn't quite answer my query. I am setting a helper class for Facebook (follows the delegation pattern) . An example of one of the class methods would be: +…
Lukas
  • 694
  • 1
  • 9
  • 24
-2
votes
1 answer

Accessing unknown setter method

hey I am getting an error accessing unknown class method 'setFirstName' but I am trying to access the variable which are already synthesized here is the code.
vrn
  • 1
  • 3
-2
votes
1 answer

I get a type error when trying to run a code but it goes away when I just give a random argument. Can someone tell me why?

I am just starting to learn about OOP in python and came across class methods. I wrote a small bit of code to make sure I understood it properly. class Person: no_of_people = 0 def __init__(self, name): self.name = name …
-2
votes
1 answer

Why can instantiating one instance of a class change the result of a method called on another instance?

When i was trying to use example from textbook and trying to rearrange some lines of code for executing, I found the result was not what I expected. There is a ''class'' which looks like below. class Circle: """This class creates circle""" …
joejoe
  • 1
-2
votes
1 answer

self.__class__ in parent class' method is missing arguments

A package I am using contains the class A. I want my new class B to inherit from this class. Minimal example: class A(): def __init__(self,name): self.name = name def my_method(self): return self.__class__(name =…
-2
votes
1 answer

Understanding class method in python

I have a three question to ask in python I have written a class method inside a class and have written basic functionality When I try to call that method from class itself and when I try to print I get the output as "None" Also if someone can…
Anshul Thakur
  • 69
  • 1
  • 6
-2
votes
1 answer

Accessing class variables outside the class in python

I have a tkinter class. I want to access the value of entry field outside the class. I have tried doing it by creating a function but it is printing the address and not the value. Here is my code class first: def __init__(self, root): …
Anmol
  • 57
  • 1
  • 7
-2
votes
9 answers

Why we do not need to call Static Methods through the object?

public static void callit(ref int var) { var++; } public static void main(Object sender, EventArgs e) { int num=6; callit(ref num); Console.WriteLine(num); } But if here method callit() would not be a static then I had…
avirk
  • 3,050
  • 7
  • 38
  • 57
-2
votes
2 answers

How do instance variables related to classes or objects in Java?

Is instance variable in class, the value of reference of class or object? I am little bit confused on call by reference.
-2
votes
1 answer

How can I return a list of class attributes in Python?

I have created a class Students that has 14 attrs. There are 20 members of the class that has 14 attrs. One of the attrs is self.project. This was initialized under: def __init__(self, name, gender, test1, project, final, midterm.....etc): …
-2
votes
1 answer

How to use method without instance initialization?

Currently I am doing this to use SampleClass.method1(): output = SampleClass().method1(input_var) But instead, I want to do this: output = SampleClass.method1(input_var). How do I do this? class SampleClass(object): def __init__(self): …
Bo Peng
  • 561
  • 2
  • 8
  • 17
-2
votes
2 answers

call @classmethod from another @classmethod of another class

I have two python files as one.py class FirstClass: @classmethod def myClass(cls,first, second): return first+second two.py from one import FirstClass class SecondClass: @classmethod def herClass(cls, val1, val2): …
sunil shrestha
  • 27
  • 3
  • 11