method👊v/s👊 function in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

method👊v/s👊 function in python

Is 'close' a method or function? As I read online that it is function BUT -------- myfile=open('filename.txt','r') #doing some operations on its contents... myfile.close() --------- here it is being called on an OBJECT i.e. the variable 'myfile' which is being used as reference of file 'filename.txt'... So shouldn't it be method?

1st Aug 2019, 7:36 AM
TANMAY HARSH
TANMAY HARSH - avatar
9 Answers
+ 12
TANMAY HARSH I think you're doing exactly what you seek by asking questions like this to expand your knowledge. Another way to really go deep is to attempt answering questions that may require you to do some research. For example, in this question, I didn't actually quite understand how a function object could be used as a function invocation while not being used as a constructor. I did a quick search and saw a reference to __call__ and decided to try calling it directly, as I did in my code sample. I then decided to print the types of the 3 different functions / methods to see if there were different kinds of function objects, which turned out to be the case. I actually discovered this a while ago. But it's about building onto your knowledge. Also, if possible, having a desktop IDE that supports breakpoints for live debugging can be an incredible tool for learning. You can examine so much about execution, types, members, state, call stack, etc. Hope this helps.
3rd Aug 2019, 4:41 PM
David Carroll
David Carroll - avatar
+ 8
James Expanding on your code snippet, the function named `add` is, in fact, an object instance of a class called function. TANMAY HARSH You might be asking yourself... How can an object `add` be invoked like a function `add(2, 2)`? It looks like a constructor... right? In reality, the function object has a method called `__call__` that is actually being invoked. Likewise, the following line will produce the same results: add.__call__(2, 2) There are class types for: - function - method-wrapper - builtin_function_or_method Run the code below using James' code snippet. https://code.sololearn.com/cs6LLT3BOL7b/?ref=app
3rd Aug 2019, 8:50 AM
David Carroll
David Carroll - avatar
+ 6
You're right, it should be called method. But this is often mixed up. Because after all, the difference is not all that great: A method is just a function that is associated with a class.
1st Aug 2019, 7:46 AM
HonFu
HonFu - avatar
+ 4
James, sounds technically correct. But I guess in the end 'method' is a word to conveniently describe a function-like thingy that is accessed via an instance or a class, like with 'class.stuff' notation. In Python, you could define a function (that would be an object, but let's call it free)... def f(): some code here ... and store a reference inside a class. class C: method = f Then you could create instances and access the free function f: instance = C() instance.method() Suddenly f has become a method. So it probably makes sense to use the words 'method' or 'function' depending on the context within the code.
1st Aug 2019, 8:07 AM
HonFu
HonFu - avatar
1st Aug 2019, 8:17 AM
TANMAY HARSH
TANMAY HARSH - avatar
+ 3
Well now using that logic since everything in Python is technically an object. You would have to say that python has methods and no functional support.
1st Aug 2019, 7:55 AM
James
James - avatar
+ 3
Yeah I was just messing around. Functions are objects too is what I was going to go at there. TANMAY HARSH try: def add(x,y): print(x+y) list = [add] list[0](2,2) because its an object you can call functiona from your other data types, ie..list, dict, etc
1st Aug 2019, 8:10 AM
James
James - avatar
+ 1
Thanks David Carroll Pardon me if I am going to ask off-topic but learning from sololearn doesn't provide the deep insight of language.....as you (and other people also) beautifully explained above, please guide me so that I'm not just limited in writing some codes only.. instead of diving into the deep knowledge-ocean of Python(I don't want to be limited to only understanding syntax and writing small codes)...(also I'm a newbie to programming and also please see my python learning progress of sololearn that may help you to provide me some tips) Thanks...
3rd Aug 2019, 11:41 AM
TANMAY HARSH
TANMAY HARSH - avatar
+ 1
David Carroll THANKS THANKS VERY MUCH....telling me about how you thought (and explaining what you mean by research based things in 3rd and 4th paragraph of your answer) and motivating me and interaction with you is going to remove the brittleness of my path towards learning Python... Just trying my best...
3rd Aug 2019, 4:52 PM
TANMAY HARSH
TANMAY HARSH - avatar