Reputation: 81
I am trying to achieve the following case:
class ABCD(object):
def __str__(self):
return "some string"
a=ABCD()
print a
some string
Using type:
A=type("A",(object,),dict(__str__="some string",h=5))
a=A()
print a
But get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Upvotes: 1
Views: 50
Reputation: 402814
Did you mean to pass a callback? __str__
is to be implemented as an instance method that returns a string.
A = type("A",(object,), dict(__str__=lambda self: "some string" , h=5))
a = A()
print(a)
some string
When you call print
on an object, its __str__
method is called. If the object does not have one defined, the the __str__
for the object
class is invoked. So, you shouldn't assign a string to __str__
because python'll try to "call" it as a function, throwing a TypeError
.
Upvotes: 4