So start with the question i.e. Why and how are Python functions hashable? First, one should know what actually hashable means in Python. So, hashable is a feature of Python objects that tells if the object has a hash value or not. If the object has a hash value then it can be used as a key for a dictionary or as an element in a set.

An object is hashable if it has a hash value that does not change during its entire lifetime. Python has a built-in hash method ( __hash__() ) that can be compared to other objects. For comparing it needs __eq__() or __cmp__() method and if the hashable objects are equal then they have the same hash value. All immutable built-in objects in Python are hashable like tuples while the mutable containers like lists and dictionaries are not hashable.

Objects which are instances of the user-defined class are hashable by default, they all compare unequal, and their hash value is their id().

**Example: **Consider two tuples t1, t2 with the same values, and see the differences:

## Python3

`t1 ` `=` `(` `1` `, ` `5` `, ` `6` `)` `t2 ` `=` `(` `1` `, ` `5` `, ` `6` `)` `# show the id of object` `print` `(` `id` `(t1))` `print` `(` `id` `(t2))` |

*chevron_right*

*filter_none*

**Output:**

140040984150664 140040984150880

In the above example, two objects are different as for immutable types the hash value depends on the data stored not on their id.

**Example: **Let’s see lambda** **functions are hashable or not.

## Python3

`# create a one-line function` `l ` `=` `lambda` `x : ` `1` `# show the hash value` `print` `(` `hash` `(l))` `# show the id value` `print` `(` `id` `(l))` `# show the hash value` `print` `(l.__hash__())` |

*chevron_right*

*filter_none*

**Output:**

-9223363246992694337 140637793303544 -9223363246992694337

Hence, lambda functions are hashable.

**Example: **Let’s see user defined def based function are hashable or not.

## Python3

`# create an empty function` `def` `fun(): ` ` ` `pass` `# print types of function ` `print` `(` `type` `(fun))` `# print hash value` `print` `(fun.__hash__())` `# print hash value` `print` `(` `hash` `(fun))` |

*chevron_right*

*filter_none*

**Output:**

<class 'function'> -9223363242199589441 -9223363242199589441

Therefore, any user defined function is hashable as its hash value remains same during its lifetime.

## Recommended Posts:

- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Why is python best suited for Competitive Coding?
- Why Python is called Dynamically Typed?
- Why import star in Python is a bad idea
- 12 Reasons Why You Should Learn Python in 2019
- Why is Python the Best-Suited Programming Language for Machine Learning?
- Why do people prefer Selenium with Python?
- Why Python Is Used For Developing Automated Trading Strategy?
- 10 Reasons Why You Should Choose Python For Big Data
- Why do we Need Immutables in Python ?
- Why is Numpy faster in Python?
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python | startswith() and endswith() functions
- Python program to count upper and lower case characters without using inbuilt functions

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.