I want to change the string representation of a python function to be just the function name.
Eg for some function
def blah(x):
...
str(blah)
currently gives
<function blah at 0x10127b578>
So I replace __str__
like this:
blah.__str__=lambda: 'blah'
but it doesn't get called by str(blah)
.
Is it possible to change __str__
for a function?
Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object. This method must return the String object.
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.
repr() compute the “official” string representation of an object (a representation that has all information about the object) and str() is used to compute the “informal” string representation of an object (a representation that is useful for printing the object).
def str(self): is a python method which is called when we use print/str to convert object into a string.
As noted already, the answer is no. If you just want to get the name of a function as a string, you can use blah.__name__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With