Python Object-Oriented Python Instant Objects Method Interactivity

Ingrid Matthews
Ingrid Matthews
1,465 Points

What 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
MOD
Victor Mercier
Treehouse Moderator 13,699 Points

Hi 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
Ingrid Matthews
1,465 Points

Thanks 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?