Menu

What Happens When We Define the Same Function Multiple Times? In Python
📰

What Happens When We Define the Same Function Multiple Times? In Python

DEV Community: beginners·Mahalakshmi C·about 1 hour ago
#rqunoMTE
#devtobeginners#devto#dev#function#python
Reading 0:00
15s threshold

What Happens When We Define the Same Function Multiple Times?

In Python, if we define the same function name more than once, the latest function definition replaces the previous definition.

When we call that function, Python executes the latest definition.

Example:

def greet ( ) : print ( "Hello" )

def greet ( ) : print ( "Welcome to Python" )

greet ( )

Enter fullscreen mode

Exit fullscreen mode

Output:

Welcome to Python

Enter fullscreen mode

Exit fullscreen mode

Explanation:

First, we define the greet() function, which prints "Hello".

Next, we define another function with the same name greet() .

The second definition replaces the first one.

When we call greet() , Python executes the second function.

Conclusion

In Python, a function name can be defined multiple times, but only the latest definition is available when the function is called.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.