This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by LinguisticsStudent
almost 11 years

Correct grammer for method in python

When I was doing the exercise to call the read method on the variable website, it should be written as, website.read().

I respect the way here. Meanwhile, I am confused. Sometimes, when we call a method, we write method(object). And sometimes, like in this example, we write object.method().

Can someone be kind enough to explain these twins?

  • Why there are two ways to call different methods?
  • Is there any difference between the two ways to call different methods?

Apparently each method uses a different way to be called, and I just want to see if there is a rule to follow, so it is easier to distinguish them, thus, it is faster to remember which one uses which way.

Thanks!

Answer 51bc9bc39c4e9d067e00ed53

3 votes

Permalink

I believe method(object) will only work if the method in question is built into Python or was imported from a module. For example, using the import statement from urllib2 import urlopen will allow you to make a call to the urlopen method like so: urlopen("http://example.com/").

object.method() is used when the method being called is a property of the object in question. read() isn’t built into Python, nor was it imported from a module, but it is a method that exists in the object stored in website. Going back to the previous example, we could have alternatively made the import statement import urllib2 and then called the urlopen method like so: urllib2.urlopen("http://example.com/")

points
Submitted by Andrew Goldin
almost 11 years

6 comments

LinguisticsStudent almost 11 years

thank you, Andrew. You explained library import very well. My question is more about different methods calling in different ways.

Andrew Goldin almost 11 years

Ah, I think I understand what you mean. I suppose the biggest difference between the two is this: In the first way, i.e. method(object), you are making a call to a built-in or imported method with the given object as the parameter (or input) to the method. In the second way, i.e. object.method(), you are making a call to a method that is a property of the object in question. That is to say, the first way is invoking a method ON an object, while the second is invoking a method OF an object. This is part of the reason why “website.read()” will work and “read(website)” will not. Python needs to know from what context you are calling the “read” function, and since it is not built in to Python or imported from anywhere, it needs to be that of an existing object, and should be called using dot notation.

The “Introduction to Classes” section of the Python track makes this idea much clearer: http://www.codecademy.com/courses/python-intermediate-en-WL8e4?curriculum_id=4f89dab3d788890003000096

LinguisticsStudent almost 11 years

Thanks Andrew, for all the explanation and looking up work. I will check the tract out later. A clearer version for my question is like, how to distinguish the methods? Which one is on an object? Which one is of an object? Should I remember them by heart? Or is there a rule to follow?

Andrew Goldin almost 11 years

No problem! In short, “method(input)” is a built-in or imported method that performs a function by taking data (a variable, an object, a number, a string, etc) as input, analyzing it, and returning a result, usually without modifying the input itself. “object.method(input)” can do the same exact thing, but require dot notation because these methods are not built in to Python. In this regard, the “object” in this method call is NOT the input to the method. It just means that that particular method is defined in that object’s code, and that is where it needs to be accessed from (a concept that is explained very clearly in the “classes” section of the Python track). Additionally, these methods can also be used to change or modify the properties of the object they are being called from. But that depends entirely on how each of those methods is written and what they are designed to do. I hope this explanation makes some sense. Otherwise I’m not too sure how else to clarify it…

LinguisticsStudent almost 11 years

I think you explained it very well. Method On an object is sort of “global”, and method Of an object is kinda “local”, if I am understanding you correctly.

LinguisticsStudent almost 11 years

And…thanks again! I appreciate it!