Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper term for the methods defined inside of a Python class before the class is instantiated?

Using this example from http://docs.python.org/tutorial/classes.html#class-objects:

class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'

According to those docs, f is an attribute reference that returns a function object.

Is there any shorter way of saying what f is? Can I call it a method of a class (note how I didn't say "class method" which is incorrect)? Or a function defined within a class? Or an instance method?

In other words, what's the formal short-hand term for f in terms of its relation to MyClass?

like image 597
coffee-grinder Avatar asked Jan 20 '23 22:01

coffee-grinder


1 Answers

If you're referring specifically to the f returned by MyClass.f, then f is an unbound method of MyClass. Or at least that's what the REPL calls it:

>>> MyClass.f
<unbound method MyClass.f>

In general though, I don't think anyone would fault you for simply calling it a "method", plain and simple. Or, in terms of its relation to MyClass, a method of MyClass.

like image 167
senderle Avatar answered Apr 09 '23 04:04

senderle