What is an "instance method"?
Asked Answered
S

4

0

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.

Sox answered 15/9, 2017 at 1:47 Comment(11)
What exactly are you asking? It gives a definition right there, what is 'the same concept of a method of a class'?Snook
Seems like you know it. Then could you explain how "combines" make it a definition?Sox
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.Snook
What do you mean by "methods are associated with instances" ? I only know that all methods are attributes of classes in Python.Sox
If you're familiar with the older terminology, maybe this helps - stackoverflow.com/questions/11949808/…Snook
How are bound and unbound methods in the link you gave related to instance methods?Sox
If you read that question answer, it says Python3 doesn't have unbound functionsConsidering
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).Snook
This definition of instance method is synonymous with bound method from the other question.Tuscan
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'.Snook
Related: Difference between class and instance methodsVittorio
G
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.

Guimond answered 15/9, 2017 at 7:35 Comment(0)
S
5
>>> 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.

Seabrook answered 15/9, 2017 at 1:51 Comment(9)
Thanks. I would like to know the definition.Sox
@Sox You copied the definition into the question, no?Considering
@cricket_007 if you believe it is a definition, could you explain what it means?Sox
@Sox You create an instance of a class object, which has some defined methods, which are therefore instance methods. Not to be confused with class methodsConsidering
@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.Seabrook
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.Sox
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.Seabrook
Yes, more descriptions please.Sox
@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.)Seabrook
P
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.

Plutonic answered 15/9, 2017 at 1:59 Comment(8)
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?Sox
Class methods are not instance methods. 'static' methods are not instance methods.Snook
@Snook Okay, but what's your point? I said "usually" not "always".Plutonic
@ChristianDean I was replying to the question by the poster in the comment. 'What kind of methods are not instance methods'.Snook
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.Snook
@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.Sox
'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.Snook
@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.Sox
G
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.

Guimond answered 15/9, 2017 at 7:35 Comment(0)
V
1

An instance method:

  • can call instance and class variables and instance, class and static methods by self.

  • can call class variables and instance, class and static methods by class name but not instance variables.

  • can be called by object.

  • can be also called directly by class name but when called directly by class name, we need to pass one argument to the instance method because self becomes the normal parameter which doesn't have the ability to call instance and class variables and instance, class and static methods.

  • needs self for the 1st argument otherwise the instance method cannot be called by an object but the instance method can still be called directly by class name and the name of self is used in convention so other names instead of self still work.

*I also explain about @classmethod and @staticmethod in my answer for @classmethod vs @staticmethod in Python.

For example, the instance method can call the instance and class variables and the instance, class and static methods by self and the instance method can call the class variable and the instance, class and static methods by class name but not the instance variables and the instance method can be called by object as shown below:

class Person:
    x = "Hello"
    def __init__(self, name):
        self.name = name
    
    def test1(self): # Instance method
        print(self.name) # Instance variable by "self"
        print(self.x)    # Class variable by "self" 
        self.test2()     # Instance method by "self"
        self.test3()     # Class method by "self"
        self.test4()     # Static method by "self"
       
        print()

        print(Person.x)       # Class variable by class name
        Person.test2("Test2") # Instance method by class name
        Person.test3()        # Class method by class name
        Person.test4()        # Static method by class name

    def test2(self):
        print("Test2")
        
    @classmethod
    def test3(cls):
        print("Test3")
        
    @staticmethod
    def test4():
        print("Test4")

obj = Person("John")
obj.test1() # By object

Output:

John  # Instance variable by "self"
Hello # Class variable by "self"
Test2 # Instance method by "self"
Test3 # Class method by "self"
Test4 # Static method by "self"

Hello # Class variable by class name
Test2 # Instance method by class name
Test3 # Class method by class name
Test4 # Static method by class name

And, if the instance method tries to call the instance variable by class name as shown below:

# ...
    
    def test1(self): # Instance method
        print(Person.name) # Instance variable by class name

obj = Person("John")
obj.test1()

The error below occurs:

AttributeError: type object 'Person' has no attribute 'name'

And, the instance method can be also called directly by class name but when called directly by class name, we need to pass one argument to the instance method as shown below because self becomes the normal parameter which doesn't have the ability to call the instance and class variables and the instance, class and static methods by self:

# ...

    def test1(self): # Instance method
        print(self)

# ...

Person.test1("Test1") # Here

Output:

Test1

So, if the instance method tries to call the instance and class variables and the instance, class and static methods by self as shown below:

# ...

    def test1(self): # Instance method
        print(self.name) # Instance variable or
        print(self.x)    # Class variable or
        self.test2()     # Instance method or
        self.test3()     # Class method or
        self.test4()     # Static method

# ...

Person.test1("Test1") # Here

The errors below occur because again, self becomes the normal parameter which doesn't have the ability to call the instance and class variables and the instance, class and static methods:

AttributeError: 'str' object has no attribute 'name'

AttributeError: 'str' object has no attribute 'x'

AttributeError: 'str' object has no attribute 'test2'

AttributeError: 'str' object has no attribute 'test3'

AttributeError: 'str' object has no attribute 'test4'

And, if one argument is not passed to the instance method as shown below:

# ...

    def test1(self): # Instance method
        print(self)

# ...

Person.test1() # Here

The error below occurs:

TypeError: test1() missing 1 required positional argument: 'self'

And, the instance method needs self for the 1st argument otherwise the instance method cannot be called by object as shown below:

# ...

    def test1(): # Without "self"
        print("Test1")

# ...

obj = Person("John")
obj.test1() # Here

Then, the error below occurs:

TypeError: test1() takes 0 positional arguments but 1 was given

But, the instance method without self can still be called directly by class name as shown below:

# ...

    def test1(): # Without "self"
        print("Test1")

# ...

Person.test1() # Here

Output:

Test1

And, the name of self is used in convention in an instance method so other name instead of self still works as shown below:

# ...
             # Here
    def test1(orange): # Instance method
        print(orange.name) # Instance variable
        print(orange.x)    # Class variable
        orange.test2()     # Instance method
        orange.test3()     # Class method
        orange.test4()     # Static method

# ...

obj = Person("John")
obj.test1()

Output:

John
Hello
Test2
Test3
Test4
Volcanism answered 17/11, 2022 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.