One common way I've used to implement this particular pattern is like this:
def first(self, person):
try:
person = Person(person)
except ConstructionError as e:
pass
# Do your thing
I don't know if that will work for you or not. You want to catch whatever error Person
would generate here if you call it with an existing Person
. It's probably even better if that's something like person_factory
, as it can just return the existing object if it's already a person instead of throwing an exception.
Multimethods are just different in Python; Python lets you implement almost any kind of semantic interface you want, at the cost of extra work under the hood somewhere, and you can usually move around where this 'somewhere' is to best hide it from you.
Also... there are advantages to this... things that you see sometimes in C++, C#, and Java like this:
int sum(int a, int b) {
return (a+b)
}
float sum (float a, float b) {
return (a+b)
}
etc.
It can be replaced in Python with just
def sum(a,b):
return a+b
(though C++ and C# have auto
now that can handle things like this too, as far as I know).