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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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.
|
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...
|
by: . |
last post by:
The future will be JSython.
Can you help this project?
|
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. ...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |