471,337 Members | 1,098 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Replacing overloaded functions with closures.

Hi,

i am trying, to no avail yet, to take a C#'s overloaded functions
skeleton and rewrite it in Python by using closures.
I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-
not-java.html) that in Python we can reduce code duplication for
overloaded functions by using closures.

I do not quite understand this. Let's say we have the following simple
C# code:

int func(int i) { return i * 2; }
string func(string s) { return s + s; }
bool func(bool f) { return !f; }

I wasn't able to find a way to express this thing in closures. I even
tried to have the parameters i, s and f as parameters with default
arguments with None value in the inner function and there to check for
the None and do the needed work but i am not sure if this is the
correct solution.

Any help ?
thanks in advance.

Jul 30 '07 #1
4 1340
The closures discussed in the article are not a solution for
function overloading. They are a solution for function
composition.
Hmmm....
>
Python generally has no need for function name overloading--if
you really want it you must do it manually using runtime type
checking.

def func(obj):
if isinstance(obj, bool):
return not obj
elif isinstance(obj, int):
return obj * 2
elif isinstance(obj, basestring):
return obj + obj
else:
raise NotImplementedError('type %r not supported' % type(obj))
I have already come up with that solution but i was a little excited
from that article that i can do the same thing with closures...And get
rid of the isinstance commands.

I'm not sure what the author was getting at, exactly.
May be that point confused me too!

Thanks Neil!

Jul 30 '07 #2
king kikapu a écrit :
Hi,

i am trying, to no avail yet, to take a C#'s overloaded functions
skeleton and rewrite it in Python by using closures.
I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-
not-java.html) that in Python we can reduce code duplication for
overloaded functions by using closures.
Then you should re-read more carefully this article. While closures are
effectively a great way to reduce code duplication, they are by no mean
presented as a way to replace function overloading - or at least not
directly[1]. The idea presented here is to use closures to write
functions that will return "parameterized" functions, and (IMHE at
least) this is mostly useful in frameworks, ORMs and like.

[1] you may want to have a look at another work by the same author:
http://peak.telecommunity.com/DevCen...sitorRevisited
which introduces the RuleDispatch generic function package.

HTH
Jul 30 '07 #3

"king kikapu" <ab********@panafonet.grwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
| def func(obj):
| if isinstance(obj, bool):
| return not obj
| elif isinstance(obj, int):
| return obj * 2
| elif isinstance(obj, basestring):
| return obj + obj
| else:
| raise NotImplementedError('type %r not supported' % type(obj))
|
| I have already come up with that solution but
|...And get rid of the isinstance commands.

A sometimes alternative is a dict (untested):

func_switch = {
bool: lambda o: not o,
int: lambda i: i*2,
str: lambda s: s+s,
unicode: lambda s: s+s
}

def func(o):
try: return func_switch[type(o)]
except IndexError:
raise NotImplementedError('type %r not supported' % type(obj))

but this does not work for instances of subclasses the way isinstance does.

tjr


Jul 30 '07 #4
Ok, i see...

Thank you all :)

Jul 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

76 posts views Thread by Nick Coghlan | last post: by
44 posts views Thread by bahadir.balban | last post: by
23 posts views Thread by Timothy Madden | last post: by
2 posts views Thread by desktop | last post: by
18 posts views Thread by Tommy Nordgren | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.