I'm trying to define a simple class and instance in Python 2.7, but I'm running into trouble with __getattr__. Minimal working example below:
class MyClass:
def __init__(self,value):
self.a = value
def __getattr__(self,name):
return 'hello'
class MyOtherClass:
def __init__(self,value):
self.a = value
MyInstance = MyClass(6)
MyOtherInstance = MyOtherClass(6)
Now if I enter dir(MyInstance)
I get:
TypeError: 'str' object is not callable
But if I enter dir(MyOtherInstance)
I get:
['__doc__', '__init__', '__module__', 'a']
Likewise if I enter MyInstance
I get:
TypeError: 'str' object is not callable
But if I enter MyOtherInstance
I get:
<__main__.MyOtherClass instance at 0x0000000003458648>
The behaviour with MyOtherInstance
is what I expect. Why am I not getting this behaviour with MyInstance
?