Skip to content

Clarification of method definition #96154

@mkaizad

Description

@mkaizad

I've come across two definitions of the term "method" in the docs:

  1. In the glossary:

    method
    A function which is defined inside a class body.

  2. In the last paragraph of 4.7 Defining Functions:

    A method is a function that ‘belongs’ to an object and is named obj.methodname, where obj is some object (this may be an expression), and methodname is the name of a method that is defined by the object’s type.

Could someone please clarify if these two definitions are equivalent?

Definition 2) seems to say that a method is a function that is bound to a particular instance of the class in which that function is defined. On the other hand, definition 1) seems to apply the term to functions defined in the class body (with no requirement that they be bound to an instance of that class).

Example:

class Foo(object):
    def hello(self):
        print('Hello')

f1 = Foo()

print(type(f1.hello))     # <class 'method'>
print(type(Foo.hello))    # <class 'function'>

Definition 2) is more consistent with what I see in the console, i.e. f1.hello is of type method because it belongs to the instance f1 and Foo.hello is of type function because it is not bound to any particular instance.

But definition 1) makes it seem like both, f1.hello and Foo.hello, should be of type method since hello is defined in Foo's body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions