views:

204

answers:

2

can anyone, please, explain to me in very simple terms what "method" is in Python?

The thing is in many Python tutorials for beginners this word is used in such way as if the beginner already knew what method is in the context of Python. While I am of course familiar with the general meaning of this word, I have no clue what this term means in Python. So,please, explain to me what the "pythonian" method s all about.

Some very simple example (code) would be very much appreciated as a picture is worth thousand words.

Thank You in advance.

+8  A: 

It's a function which is a member of a class:

class C:
    def my_method(self):
        print "I am a C"

c = C()
c.my_method()  # Prints "I am a C"

Simple as that!

(There are also some alternative kinds of method, allowing you to control the relationship between the class and the function. But I'm guessing from your question that you're not asking about that, but rather just the basics.)

RichieHindle
It's worth noting here that the instance has to be manually passed into the method, and by convention it's passed as `self`.
Skilldrick
@Silldrick: You propably mean the right thing, but your comment as-is implies something wrong. The instance doesn't need to be passed explicitly/manually when calling a method on it. But the method has to recieve it explicitly, i.e. you have to manually add a parameter for the instance (which, yes, is called `self` by convention).
delnan
@delnan - Yes, thanks for the clarification - when I say manually passed in what I really mean is it needs to be explicitly received as a parameter, but it's passed in implicitly.
Skilldrick
@AaronMcSmooth: AndiDog's answer is certainly more complete. I'd be interested to know whether @brilliant finds it "better", or *too* complete. Sometimes a simple answer is better. (Because I don't like being accused of rep-whoring, I've made my answer CW.)
RichieHindle
Thank You, RichieHindle, for this answer and for this example. Truth be told, there are some elements in it, which I still need to understand (perhaps, that will be the content of my further questions), but the main thing is clear now: a method is a function - luckily I already know what a function is. Thank You again.
brilliant
@brilliant. don't let a simplistic answer confuse you. There is (slightly) more to it than "a method is a function" but the difference is absolutely crucial. Look at the other answer and post new questions for anything that you don't understand about it.
aaronasterling
RichieHindle, please don't worry about what AaronMcSmooth said. As I have already stated, Your answer and especially that simple piece of code, helped me a lot, and I really learned from it. I just hope that there are many supporters here who are willing to provide their answers to newbees the way You did. Thanks again!
brilliant
@RichieHindle. My initial reaction might have been a little rough and I suppose "rep-whoring" was out of line. It is an insulting answer though. the "that's all there is to it" line is straight up wrong: There's plenty more to it.
aaronasterling
@AaronMcSmooth: Thaks for saying this. Of course, I will do my further reasearch regarding all those things which I don't understand, but, AaronMcSmooth, I think that fact (that method is a function) I also needed to know, and ,perhaps, I needed to know that in the first place. At the moment I am studying AndiDog's answer.
brilliant
@Aaron: How is there more to `It's a function which is a member of a class`? Yes that's not the most basic answer, but it is a 100% complete answer of the question `What is a method`
Falmarri
@Falmarri. Try making a class and assigning a function to it as an attribute. Then you have a function which is a member of a class.
aaronasterling
@Falmarri. `class Foo: pass`, `def foofunc(): pass`. `f = Foo()`. `f.foofunc()` ?.
aaronasterling
I guess you're getting into technicalities now. I wouldn't call that function a member of a class. If you want to be technical, it would be a function being referenced by a class member variable.
Falmarri
Just because you can get a function to act like a class member doesn't make it a class member.
Falmarri
@Falmarri. What could possibly be meant by a "class member" other than "value referenced by a class level variable"?
aaronasterling
Yeah, the member is the reference, not what it's referring to. Take a singleton class for example. If you reference a singleton class to a member variable, you wouldn't call that singleton a class member, that doesn't make any sense.
Falmarri
@Falmarri. Well then by that definition, a method cannot possibly be a member of a class because it is itself a value on the same footing as `foo` in my example. So the given definition of method is bunk by that definition of member too. Also, it would make perfect sense to refer to the singleton as a class member. Why can't a value be a member of more than one class?
aaronasterling
I think we're arguing technicalities here.
Falmarri
I think you're trying to use an example that doesn't compile. I posted a question regarding this on http://programmers.stackexchange.com/questions/7212/definition-of-a-method
Falmarri
@Falmarri. If course it compiles. it doesn't _run_. It shouldn't run. It makes not sense because even though `fooFunc` is a member of `Foo`, and it's a function, it's not a _method_.
aaronasterling
What do you mean it compiles but doesn't run. What you submitted is a syntax error, if I'm interpreting it correct. Are you saying this? http://pastebin.org/1155936
Falmarri
@Falmarri. No I've submitted a correction. The syntax error was because you didn't indent `pass` in `fooFunc`. Either the corrected version or the one that I added (the original) will yield a `TypeError` when you run them. but in both cases `fooFunc` is a function that is a member of `Foo`.
aaronasterling
@Falmarri. This is well out of hand for a comments discussion. here's a question: http://stackoverflow.com/questions/3791651/what-constitutes-a-member-of-a-class-what-constitutes-a-method
aaronasterling
You can't use non-runnable code as an example for an argument... You can't run this code exactly because you're trying to use a function as a method.
Falmarri
+5  A: 

A method is a function that takes a class instance as its first parameter. Methods are members of classes.

class C:
    def method(self, possibly, other, arguments):
        pass # do something here

As you wanted to know what it specifically means in Python, one can distinguish between bound and unbound methods. In Python, all functions (and as such also methods) are objects which can be passed around and "played with". So the difference between unbound and bound methods is:

1) Bound methods

# Create an instance of C and call method()
instance = C()

print instance.method # prints '<bound method C.method of <__main__.C instance at 0x00FC50F8>>'
instance.method(1, 2, 3) # normal method call

f = instance.method
f(1, 2, 3) # method call without using the variable 'instance' explicitly

Bound methods are methods that belong to instances of a class. In this example, instance.method is bound to the instance called instance. Everytime that bound method is called, the instance is passed as first parameter automagically - which is called self by convention.

2) Unbound methods

print C.method # prints '<unbound method C.method>'
instance = C()
C.method(instance, 1, 2, 3) # this call is the same as...
f = C.method
f(instance, 1, 2, 3) # ..this one...

instance.method(1, 2, 3) # and the same as calling the bound method as you would usually do

When you access C.method (the method inside a class instead of inside an instance), you get an unbound method. If you want to call it, you have to pass the instance as first parameter because the method is not bound to any instance.

Knowing that difference, you can make use of functions/methods as objects, like passing methods around. As an example use case, imagine an API that lets you define a callback function, but you want to provide a method as callback function. No problem, just pass self.myCallbackMethod as the callback and it will automatically be called with the instance as first argument. This wouldn't be possible in static languages like C++ (or only with trickery).

Hope you got the point ;) I think that is all you should know about method basics. You could also read more about the classmethod and staticmethod decorators, but that's another topic.

AndiDog
Thank You, AndiDog, for this expanded answer and for Your time that You spent on it. I am studying it at the moment.
brilliant
+1 for the great explanation of bound vs. unbound.
ma3