Skip to content Skip to sidebar Skip to footer

Parentheses In Python's Method Calling

Here is a simple Python code for item in sorted(frequency, key=frequency.get, reverse=True)[:20]: print(item, frequency[item]) However, if call frequency.get() instead of freque

Solution 1:

frequency.get describes the method itself, while frequency.get() actually calls the method (and incorrectly gives it no arguments). You are right that this is different than Ruby.

For example, consider:

frequency = {"a": 1, "b": 2}
x = frequency.get("a")

In this case, x is equal to 1. However, if we did:

x = frequency.get

x would now be a function. For instance:

print x("a")
# 1print x("b")
# 2

This function is what you are passing to sorted.

Post a Comment for "Parentheses In Python's Method Calling"