473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

multimethod (or rather overloading) in Python

Dear pythonistas!

I'd like to emulate overloading in Python (like C++).

Let's consider an example:

class A(object): pass
class B(object): pass

Of course, there are some standard overloading implementations for
Python. For example:

def overload(cls):
def wrapper(f):
gl = f.func_globals

next = gl.get(f.func_name, failedOverload_(f.func_name))
def _(obj, *args, **kwargs):
if isinstance(obj, cls):
return f(obj, *args, **kwargs)
else:
return next(obj, *args, **kwargs)
return _

@overload(A)
def foo(_): print 'foo@A'
@overload(B)
def foo(_): print 'foo@B'

However, it obviously doesn't work for classes: I cannot overload
instance methods with such a decorator.

The best way I found is closures. Unfortunatley, in this case I need a hack:

gl = f.func_globals

turns into:

gl = sys._getframe(1).f_locals

and with this hack one can make the following trick:

def poorManOverloadedMethods(someInfo):
@overload(A)
def _(_): print '%s: poorManOverloadedMethods@A' % someInfo

@overload(B)
def _(_): print '%s: poorManOverloadedMethods@B' % someInfo

return _

PMOM = poorManOverloadedMethods('test')

....

Of course, I can imagine some metaclasses magic that would allow to code:

class MyClass(WithOverloading):
@overloadMethod(A)
def someMetod(self, _): ...

But it would rather convoluted: the best idea I have so far is to mangle
methods name in the manner most of C++ compilers do.

Is there better way? Can I unify both @overload and @overloadMethod?

with the best regards,
anton.
Jul 18 '05 #1
3 1672
anton muhin wrote:

Correction:
Of course, I can imagine some metaclasses magic that would allow to code:

class MyClass(WithOverloading):
@overloadMethod(A)
def someMetod(self, _): ...

But it would rather convoluted: the best idea I have so far is to mangle
methods name in the manner most of C++ compilers do.


Stupid me. Of course, name magling is impossible and unnecessary. Sorry.

Still the question remains.

with the best regards,
anton.
Jul 18 '05 #2
anton muhin wrote:
anton muhin wrote:

Correction:
Of course, I can imagine some metaclasses magic that would allow to code:

class MyClass(WithOverloading):
@overloadMethod(A)
def someMetod(self, _): ...

But it would rather convoluted: the best idea I have so far is to
mangle methods name in the manner most of C++ compilers do.


PEAK has a fairly sophisticated implementation of method dispatch you may want
to look at.

http://peak.telecommunity.com/Articles/WhatisPEAK.html
http://dirtsimple.org/2004/11/generi...ve-landed.html
http://peak.telecommunity.com/doc/sr.../__init__.html

I'm compelled to point out that PEAK should still be considered a 'work in
progress', but PJE's ideas should help you out :)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #3
Nick Coghlan wrote:
anton muhin wrote:
anton muhin wrote:

Correction:
Of course, I can imagine some metaclasses magic that would allow to
code:

class MyClass(WithOverloading):
@overloadMethod(A)
def someMetod(self, _): ...

But it would rather convoluted: the best idea I have so far is to
mangle methods name in the manner most of C++ compilers do.

PEAK has a fairly sophisticated implementation of method dispatch you
may want to look at.

http://peak.telecommunity.com/Articles/WhatisPEAK.html
http://dirtsimple.org/2004/11/generi...ve-landed.html
http://peak.telecommunity.com/doc/sr.../__init__.html

I'm compelled to point out that PEAK should still be considered a 'work
in progress', but PJE's ideas should help you out :)

Cheers,
Nick.


Thank you very much, Nick!

anton.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
49
IDE
by: Thomas Lindgaard | last post by:
Hello I am probably going to start a war now... but so be it :) I just want to hear what all you guys who eat pythons for breakfast use for python coding. Currently I use Kate, but I would...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
9
by: Adriano Ferreira | last post by:
Many Python scripts I see start with the shebang line #!/usr/bin/env python What is the difference from using just #!python Regards, Adriano.
52
by: Steve Holden | last post by:
I've been thinking (and blogging) about python evangelism since PyCon, as a result of which I created a squidoo lens: http://www.squidoo.com/pythonlogy Imagine my surprise at discovering that...
4
by: . | last post by:
The future will be JSython. Can you help this project?
2
by: Larry Bates | last post by:
I have a rather large Python class that I've converted to a COM object. I can dispatch (using either Python or VB) and call the methods perfectly. Now a new client wants to call it from Delphi. ...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.