Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Function name in Python

📅 2012-May-03 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ function, name, python ⬩ 📚 Archive

The __name__ attribute of a function holds the function name. Even if the function is assigned to a new variable, its name remains the name with which it was originally defined. Lambda functions have the name <lambda>

def squareIt( x ):
    return x * x
print( squareIt.__name__ ) # squareIt

foo = squareIt
print( foo.__name__ ) # squareIt

squareIt = lambda x: x * x
print( squareIt.__name__ ) # <lambda>

foo = squareIt
print( foo.__name__ ) # <lambda>

Tried with: Python 3.2


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧