By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,627 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,627 IT Pros & Developers. It's quick & easy.

Replacing overloaded functions with closures.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
Ok, i see...

Thank you all :)

Jul 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.