Different methods with same name but different signature?

Fredrik Lundh fredrik at pythonware.com
Thu Apr 15 10:00:24 EDT 1999


frederic pinel wrote:
> While trying to implement the visitor pattern in Python, I ran into the
> following problem:
> 
> My class needs to have 2 (or more) different methods, but with the same
> name, the difference being the signature (an object in my case),
> unfortunately Pyhton interprets this as an overidding, and only
> considers the last method defined.

well, python doesn't allow you to specify argument types,
so there's no way to tell the difference anyway...

> - using a single method, but checking isinstance() of the object passed
> to switch to the right code,
> - using different names for the methods,

the second method is preferred -- it's faster, easier to
understand, and less error-prone

imho, it's also an improvement over the pure visitor pattern,
since it allows you to generate "logical events" that doesn't
correspond to "physical" data instances in your model.

</F>





More information about the Python-list mailing list