Signature-based Function Overloading in Python
Michael Rudolf
spamfresser at ch3ka.de
Thu Feb 25 08:44:20 EST 2010
Am 25.02.2010 11:58, schrieb Jean-Michel Pichavant:
>> You said it yourself: "simply make two or three functions and name
>> them appropiately" :-)
>>
>> When 2 methods of a class were to have the same name for doing
>> completely different things like you said, there's a design flaw to my
>> opinion.
>>
>> JM
> I wonder if I've just written that my opinion is flawed... It surely is,
> but that's not why I meant :-)
:D
Well, there might be cases when someone wants to do this. consider:
(pseudocode - this is *not* python ;)
class Machines (Object):
@classmethod
def shutdown(cls, Machine, emergency=False):
try:
if Machine is instanceof(Fileservers):
if not emergency:
Machine.unmount_raid_first()
...
Machine.halt()
if Machine is instanceof(Router):
if not emergency:
cls.shutdown(Machine.attachedmachines)
...
...
finally:
if emergency and Machine has powerswitch:
Machine.powerswitch.Off()
@classmethod
def emergency(cls):
for machine in cls.instances:
cls.shutdown(machine, 1)
Other design patterns might me good too, but I like the idea of having
emergency protocols in *one* place here. But without method overloading,
there are many many nested 'if's.
One could say that the machines have to know how to shut down itself,
but there might be dependencies they do not know about, and as said, it
clutters the emergency protocol all over the modules.
One could define normal_shutdown() and emergency_shutdown(), but I do
not like this eighter, and there are still many 'if's to seperate the types.
There are cases where *no* solution is perfect, and those are the cases
where "2 methods of a class were to have the same name for doing
completely different things" or other messy things like this are IMO
*NOT* a design flaw.
Regards,
Michael
More information about the Python-list
mailing list