Questions tagged [instance-methods]
180 questions
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
431
votes
9 answers
Why do I get "TypeError: Missing 1 required positional argument: 'self'" trying to call a method from the class directly?
I have some code like:
class Pump:
def __init__(self):
print("init")
def getPumps(self):
pass
p = Pump.getPumps()
print(p)
But I get an error like:
Traceback (most recent call last):
File…

DominicM
- 6,520
- 13
- 39
- 60
28
votes
2 answers
Call static method from instance in PHP, future deprecation?
While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For…

Dan Lugg
- 20,192
- 19
- 110
- 174
22
votes
4 answers
Extension Methods vs Instance Methods vs Static Class
I'm a little bit confused about the different ways to use methods to interact with objects in C#, particularly the major design differences and consequences between the following:
Invoking an instance method
Using a static class on a POCO
Creating…

johnnyRose
- 7,310
- 17
- 40
- 61
19
votes
5 answers
How to make len() work with different methods on different instances of a class, without modifying the class?
Is there a way to make len() work with instance methods without modifying the class?
Example of my problem:
>>> class A(object):
... pass
...
>>> a = A()
>>> a.__len__ = lambda: 2
>>> a.__len__()
2
>>> len(a)
Traceback (most recent call last):
…

ARF
- 7,420
- 8
- 45
- 72
18
votes
2 answers
Referring to javascript instance methods with a pound/hash sign
This question is similar to Why are methods in Ruby documentation preceded by a hash sign?
I understand why in Ruby instance methods are proceeded with a pound sign, helping to differentiate talking about SomeClass#someMethod from…

Josh
- 10,961
- 11
- 65
- 108
17
votes
3 answers
Ruby: Can I use instance methods inside a class method?
I have a class that contains this class method:
def self.get_event_record(row, participant)
event = Event.where(
:participant_id => participant.id,
:event_type_code => row[:event_type],
:event_start_date =>…

steve_gallagher
- 3,778
- 8
- 33
- 51
15
votes
3 answers
Ruby modules and extend self
In what sort of situation is the code:
module M
extend self
def greet
puts "hello"
end
end
more beneficial to use over say something like:
module M
def self.greet
puts "hello"
end
end
In the top, one is an instance method…

joeellis
- 2,745
- 7
- 28
- 45
14
votes
2 answers
Ruby Unbound Methods: Is it possible to force bind to instances of other classes?
I would like to know if I could force this to happen
class A
def bomb ; "bomb" ; end
end
class B ; end
bomb = A.instance_method(:bomb)
b = B.new
bomb.bind(b)
currently it throws the error TypeError: bind argument must be an instance of A
I…

ChuckE
- 5,610
- 4
- 31
- 59
12
votes
5 answers
Static variables in instance methods
Let's say I have this program:
class Foo {
public:
unsigned int bar () {
static unsigned int counter = 0;
return counter++;
}
};
int main ()
{
Foo a;
Foo b;
}
(Of course this example makes no sense since I'd…

Davide Valdo
- 779
- 8
- 21
12
votes
2 answers
How can you call class methods on mailers when they're not defined as such?
When sending mail in Rails, usually one would do something like this:
UserMailer.password_reset(user).deliver
But if we look inside UserMailer we can see this:
def password_reset(user) # not self.password_reset
# ...
end
Notice that the method…

dee
- 1,848
- 1
- 22
- 34
11
votes
3 answers
In Ruby are there any related applications of the syntax: class << self ... end
class << self
attr_accessor :n, :totalX, :totalY
end
The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for…

pez_dispenser
- 4,394
- 7
- 37
- 47
11
votes
4 answers
Java create a unique ID for each instantiated object using instance methods instead of class/static methods
Quite new to this so I hope I have the terminology in the title right.
I am trying to figure out how to create an instance method that will do the following:
--An ID number is returned.
--As each object is created from the class…

SeesSound
- 503
- 4
- 13
- 24
10
votes
5 answers
Adding attributes to instance methods in Python
I would like to add an attribute to an instance method in one of my classes. I tried the answer given in this question, but this answer only works for functions -- as far as I can tell.
As an example, I would like to be able to do something…

sbell
- 457
- 6
- 13
10
votes
4 answers
Static and instance methods in Python
Can I define a Python method to be both static and instance at the same time? Something like:
class C(object):
@staticmethod
def a(self, arg1):
if self:
blah
blah
So that I can call it with…

xster
- 6,269
- 9
- 55
- 60