too dynamic - how to avoid?
Alexander Schmolck
a.schmolck at gmx.net
Thu Oct 2 06:36:38 EDT 2003
"mic" <aldo123 at onet.pl> writes:
> I've spent last hour trying to debug my code, when I found out that instead
> of executing object's method I accidentaly overridden it with variable (see
> below example). My stupid! But as the system becomes more complex, I begun
> to wonder how to avoid such situations in the future. Does anybody have some
> experiences about that?
- use of decent naming schemes presumably helps (and if your class gets so big
that you can't remember what are attributes and what are methods refactoring
would probably be a good idea)
- if that's not enough you can always do something like override __setattr__,
to check that you don't overwrite instance methods, e.g. (UNTESTED):
import types
class bar:
...
def __setattr__(self, attr, value):
if isinstance(getattr(self, attr, None), types.MethodType):
raise RuntimeError("Trying to overwrite method %s" % attr)
else setattr(self, attr, value)
- if you need this often, you could write a function or metaclass to avoid
duplicating the above in all classes where you feel you need it.
'as
More information about the Python-list
mailing list