Methods/Functions must be called with parentheses ()
. Attributes/Properties can be retrieved without parentheses. And there is a weird third case which I'll get into later.
Consider this example
foo.bar()
foo.baz
In typical usage bar
would be a function/method of foo. baz
would be some kind of attribute/property/parameter of foo.
In your case, upper
is actually a function/method and should normally called with parentheses ()
. Consider this interactive result
>>> a = 'Hello World'
>>> a
'Hello World'
>>> a.upper()
'HELLO WORLD'
>>> a.upper
<built-in method upper of str object at 0x000001EC70EFAD70>
When you call a.upper()
it works as you might expect. But when you call a.upper
it says you're getting a method. I make this mistake all of the time when I forget to put parentheses on a function call. I don't know how common it is for other people.
Coming back to a.upper
. With that, you're getting the actual function/method instead of the return value of the function/method. There are some cases where it can be useful to pass a function to another class or pass a function to another function. You can also use this feature to make your own name for a function. Consider
>>> a = 'Hello World'
>>> alias = a.upper
>>> alias()
'HELLO WORLD'
>>> alias
<built-in method upper of str object at 0x000001EC70EFAFB0>
I've assigned a.upper
to something new called alias
. And when I call alias()
, then it returns the upper case of a.