Code Explanation:
๐น 1. Function Definition
def show():
return "Hi"
✅ Explanation:
A function named show() is created.
When called:
show()
it returns:
"Hi"
⚠️ Important:
At this point:
show
means the function object itself, not the return value.
๐น 2. Creating Dictionary
d = {
show: 100
}
✅ Explanation:
A dictionary is created.
Key:
show
Value:
100
Dictionary Internally
It looks like:
{
<function show>: 100
}
⚠️ Important:
The key is NOT:
"Hi"
and NOT:
show()
The key is the actual function object.
๐น 3. Why Function Can Be a Key?
Functions in Python are objects.
Example:
print(type(show))
Output:
<class 'function'>
Since functions are hashable objects,
they can be used as:
Dictionary keys ✅
Set elements ✅
๐น 4. Accessing Dictionary Value
print(d[show])
✅ Explanation:
Python searches for key:
show
inside dictionary.
Dictionary contains:
show : 100
So Python finds:
100
๐น 5. Printing Result
print(d[show])
prints:
100
๐ฏ Final Output
100

0 Comments:
Post a Comment