Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialIngrid Matthews
1,465 PointsWhat is the difference between function(thing) and thing.function()?
This is a basic question, I'm not referring to a specific code here. Trying to understand these two kinds of syntax.
1 Answer
Victor Mercier
14,667 PointsHi Ingrid,
In the first case, you are passing an argument to a function For example:
# Define a function `plus()`
def plus(a,b):
return a + b
To call the function, you would do plus(2,3)
In the second case, you are calling a method on an object: For example:
# Create a `Summation` class
class Summation(object):
def sum(self, a, b):
self.contents = a + b
return self.contents
To call the function on the method, you would be doing Summation.sum(1,2) If that helped you , do not forget to mark as best answer to indicate your issue is resolved!
Ingrid Matthews
1,465 PointsIngrid Matthews
1,465 PointsThanks so much. I still don't see the use of both the dot and the parentheses together ( thing.function() ).
In your example, if the contents of self are a + b, are there 3 parameters in the method (a, b, and a+b)?
I understand that the first example is calling a function. Is it right that a method is a type of function? But if it's a method (that is, within a class) it's called with the dot? What would empty parentheses be for?