Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 30th, 2007, 04:05 PM
king kikapu
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 30th, 2007, 04:45 PM
king kikapu
Guest
 
Posts: n/a
Default Re: Replacing overloaded functions with closures.

The closures discussed in the article are not a solution for
Quote:
function overloading. They are a solution for function
composition.
Hmmm....
Quote:
>
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.

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

Thanks Neil!

  #3  
Old July 30th, 2007, 05:05 PM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default Re: Replacing overloaded functions with closures.

king kikapu a écrit :
Quote:
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
  #4  
Old July 30th, 2007, 06:15 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: Replacing overloaded functions with closures.


"king kikapu" <aboudouvas@panafonet.grwrote in message
news:1185810128.237758.17550@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




  #5  
Old July 31st, 2007, 09:05 AM
king kikapu
Guest
 
Posts: n/a
Default Re: Replacing overloaded functions with closures.

Ok, i see...

Thank you all :)

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles