Reputation: 26048
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 frequency.get
, it will give me the error of "get expected at least 1 arguments, got 0"
I came from Ruby. In Ruby get
and get()
would be exactly the same. Is it not the same in Python?
For example, here is http://www.tutorialspoint.com/python/dictionary_get.htm the description of get()
and not get
. What is get
?
Upvotes: 0
Views: 168
Reputation: 78660
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")
# 1
print x("b")
# 2
This function is what you are passing to sorted
.
Upvotes: 9