-2

From 3. Data model:

Instance methods

An instance method object combines a class, a class instance and any callable object (normally a user-defined function).

If it is a definition, what does it mean?

If it is not a definition, what is the definition of an "instance method"?

Is an "instance method" the same concept of a method of a class?

Since someone brings up class methods and static methods, bound methods and unbound methods, let me clarify:

I understand a method of a class can be an ordinary method, a class method, or a static method. I understand a method of a class accessed via the class or its instance can be bound or function. I have never heard of "an instance method". I don't know what it is even after looking at the quote and am not sure if it is related to a ordinary method, a class method, or a static method, or a bound method or function.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Tim
  • 88,294
  • 128
  • 338
  • 543
  • 1
    What exactly are you asking? It gives a definition right there, what is 'the same concept of a method of a class'? – pvg Sep 15 '17 at 01:48
  • 2
    Seems like you know it. Then could you explain how "combines" make it a definition? – Tim Sep 15 '17 at 01:51
  • I guess I don't undestand how the word 'combines' makes something not a definition. it's basically a regular method - methods are associated with instances. It's to distinguish it from a class method which is associated with a class. Widely used term in most OO languages. – pvg Sep 15 '17 at 01:52
  • What do you mean by "methods are associated with instances" ? I only know that all methods are attributes of classes in Python. – Tim Sep 15 '17 at 01:54
  • If you're familiar with the older terminology, maybe this helps - https://stackoverflow.com/questions/11949808/what-is-the-difference-between-a-function-an-unbound-method-and-a-bound-method – pvg Sep 15 '17 at 01:54
  • How are bound and unbound methods in the link you gave related to instance methods? – Tim Sep 15 '17 at 01:55
  • If you read that question answer, it says Python3 doesn't have unbound functions – OneCricketeer Sep 15 '17 at 01:56
  • Methods are dispatched based on (in other languages implicit, in python explicit) first param, right. That param (self) is the instance of the class, in an instance method. Hence, instance method (as opposed to a class method, which gets the class as its param). – pvg Sep 15 '17 at 01:57
  • This definition of instance method is synonymous with bound method from the other question. – ShadowRanger Sep 15 '17 at 01:57
  • 1
    Maybe part of the confusion arises from the fact that the quoted bit from the doc is describing the actual _object_ python uses to represent an instance method, a concrete thing as opposed to an abstraction like 'instance method'. – pvg Sep 15 '17 at 02:05
  • Related: *[Difference between class and instance methods](https://stackoverflow.com/questions/17134653/)* – Peter Mortensen Jan 15 '22 at 14:25

3 Answers3

4
>>> class Foo:
...     def im_a_method(self):
...         pass
... 
>>> x = Foo()
>>> x.im_a_method
<bound method Foo.im_a_method of <__main__.Foo object at 0x7f4f1993dd30>>

Tada! That's an instance method object. It's the thing you get when you retrieve a method of an object, before you call it.

user2357112
  • 235,058
  • 25
  • 372
  • 444
  • Thanks. I would like to know the definition. – Tim Sep 15 '17 at 01:52
  • @Tim You copied the definition into the question, no? – OneCricketeer Sep 15 '17 at 01:54
  • 1
    @cricket_007 if you believe it is a definition, could you explain what it means? – Tim Sep 15 '17 at 01:57
  • @Tim You create *an instance* of a class object, which has some defined methods, which are therefore *instance methods*. Not to be confused with [class methods](https://softwareengineering.stackexchange.com/questions/306092/what-are-class-methods-and-instance-methods-in-python) – OneCricketeer Sep 15 '17 at 02:00
  • 1
    @Tim: I think you may be well served by taking a break from all the books and specs you read, and writing some programs. You always seem really unfamiliar with how things work in practice, and you seem to have a lot of trouble figuring out how the information you read fits together. Writing some code could help with that. – user2357112 Sep 15 '17 at 02:12
  • Quite the opposite. I had written some code before reading the books and specs. Now it is time to read the books and specs and really understand what I was doing. – Tim Sep 15 '17 at 02:14
  • 1
    That said, what I've posted is about as much of a definition as instance method objects have. It's certainly possible to give more *description*, or show you the *implementation*, but a *definition*? These things aren't like iterators or abelian groups; you're not going to check whether something is an instance method object by going down a list of properties and seeing whether it fulfills them. It's like asking for a definition of "Ford Focus" - kind of a weird thing to ask, and not all that useful. – user2357112 Sep 15 '17 at 02:20
  • Yes, more descriptions please. – Tim Sep 15 '17 at 02:23
  • @Tim: Keep reading the page you were already reading. There's a whole screenful of description. ("Keep reading the page you were already reading" is good advice for a lot of your questions.) – user2357112 Sep 15 '17 at 02:27
3

What is an instance method?

An instance method is a function the is bound to a class instance. The instance of the class is implicitly passed as the first argument to instance methods. It essentially belongs to that specific instance. An instance method is the "normal" type of method people use. This is opposed to a static method or class method created using staticmethod and classmethod respectively.

Here's an example of an instance method:

>>> class Class: 
...     def method(self): 
...        pass 

>>> Class.method
<bound method Class.method of <Class object at 0x7f12781c5b70>>

It's that simple.

Christian Dean
  • 20,986
  • 7
  • 47
  • 78
  • Thanks. Could you show that what you explained is the same as "An instance method object combines a class, a class instance and any callable object (normally a user-defined function)"? What methods are not instance methods? – Tim Sep 15 '17 at 02:00
  • 1
    Class methods are not instance methods. 'static' methods are not instance methods. – pvg Sep 15 '17 at 02:01
  • @pvg Okay, but what's your point? I said "usually" not "always". – Christian Dean Sep 15 '17 at 02:12
  • @ChristianDean I was replying to the question by the poster in the comment. 'What kind of methods are not instance methods'. – pvg Sep 15 '17 at 02:16
  • 1
    Downvoter here! I think this answer is just a little woolly, particularly 'a function defined inside of a class' which I _think_ actually has something to do with the poster's confusion (hard to tell, the question is woolier). But it needs sharper distinction between declarations, instances of objects in general, instances of classes, instances of class objects and maybe even the instances of the method objects the doc talks about. – pvg Sep 15 '17 at 02:24
  • @pvg: I understand a method of a class can be an ordinary method, a class method, or a static method. I have never heard of "an instance method". I don't know that it is even after looking at the quote. I hope you understand my confusion. – Tim Sep 15 '17 at 02:26
  • 1
    'instance method' is what you are describing as 'ordinary method'. That's all there is to it. It's called that because it 'belongs', in a sense, to a specific instance of a class. It is widely used terminology across many OO languages, you just hadn't come across it before. – pvg Sep 15 '17 at 02:29
  • @pvg: What do you mean " it 'belongs', in a sense, to a specific instance of a class"? A method is always an attribute of a class, so it belongs to a class, not to its instance. – Tim Sep 15 '17 at 02:31
2

Your confusion comes from what exactly this definition is about. The term "instance method" is actually used to describe both the concept (a method that works on an instance - by opposition with a classmethod or staticmethod) and its technical implementation. The definition you quote is about the technical implementation.

If you want to understand the context of this definition, you can read this article in the Python wiki, which explains how Python turns functions into methods at runtime.

pvg
  • 2,651
  • 4
  • 15
  • 31
bruno desthuilliers
  • 72,252
  • 6
  • 79
  • 103