I am trying to prevent an instance from throwing an exception if a method that does not exist for the instance is called. I have tried the following:
class myClass(object):
def __init__(self):
pass
def __getattr__(self,name):
print "Warning - method {0} does not exist for this instance".format(name)
o = myClass()
var = o.someNonExistantFunction()
The problem is that I get the following error:
TypeError: 'NoneType' object is not callable
The two things I want to make sure of doing is:
- Return
Noneas my code can deal with variables being set toNone - Perform a function (printing a warning message)
What is the cleanest way to do this?
return a function that does nothing?
Couple of things: first you might want to use
__getattr__rather than__getattribute__.__getattr__gets called when the runtime doesn’t find anything by that name in the hierarchy,__getattribute__gets called every time.