Hi,
I wonder, does Python support generic programming paradigm, and to what extent
(I guess, it doesn't do it in full)? And (or) does it move in that direction?
Will we ever see
concept WellAlright:
...
constructs in Python?
Isn't it right time to reserve "concept" as a keyword ;-)
Sincerely yours, Roman Suzi
-- rn*@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
Jul 18 '05
16 9736
I hate to break it to you Terry, but Issac did understand the question. The
example he gave was perhaps a little fanciful, but it was on the money.
That is exactly the kind of thing I was asking about.
More typical examples of specialization may be found in the Standard
Template Library, and in the Boost Graph Library.
When you define a function in Python, you are (in effect) defining many
functions, just as Issac says. What the byte codes eventually do depends on
type information that is acted on at runtime. C++ has a mechanism
(templates) for acting on the type information similarly, but at compile
time. However, a modern C++ compiler can do something beyond that. It can
recognize special cases (which the programmer must code for) and use a
different functional form (template) for the special case than for the
generic case. Both STL and BGL use the mechanism extensively.
jdadson at yahoo dott com
"Terry Reedy" <tj*****@udel.e du> wrote in message
news:ma******** *************** *************** @python.org... "Isaac To" <ik****@netscap e.net> wrote in message news:87******** ****@sinken.loc al.csis.hku.hk. ..
"Terry" == Terry Reedy <tj*****@udel.e du> writes: Terry> Specialization goes in special methods -- the ones named Terry> __xxx__ -- of which there are now 50-100. No, you don't quite understand what the OP is asking for. No, YOU are the one who does not understand, because you apparently did
not do me the courtesy of reading my entire post, in which I quoted the question I answered. Here again is the OP's question about *type* specialization *in Python*: If you have, for example, several data-types for matrices, how do you write code that will automatically find the right routine for quickly multiplying a vector by a diagonal matrix represented as a vector, and automatically call the right code for multiplying by a sparse matrix represented by some sparse coding, etc?
The has NOTHING to do with writing base cases in recursive C++ template metaprogramming (which, ironically, uses the compiler as an interpreter).
Although there may be other answers now and in the future, the answer I gave is the straightforward standard answer for Python today.
Terry J. Reedy
>>>>> "Terry" == Terry Reedy <tj*****@udel.e du> writes: No, you don't quite understand what the OP is asking for.
Terry> you apparently did not do me the courtesy of reading my
Terry> entire post, in which I quoted the question I answered.
I admit that I didn't read your mail very carefully when I write my
response. I did that immediately afterwards, and my post didn't make
sense even to myself. So I cancel that post a minute after making it,
although apparently my cancel posting didn't go as far as my response.
Terry> The has NOTHING to do with writing base cases in recursive
Terry> C++ template metaprogramming (which, ironically, uses the
Terry> compiler as an interpreter).
I see no problem using the compiler as an interpreter. Indeed, I
think C++ should make it more explicit and allow one to do things in
compile time as easily as one can do at run-time (e.g., why only
support integers, and why one have to do recursions instead of
loops?). But this is a Python group, not a C++ group, so such things
are off-topic here.
Terry> Although there may be other answers now and in the future,
Terry> the answer I gave is the straightforward standard answer
Terry> for Python today.
On the other hand, I didn't agree with this. Your answer basically
said "define a member function called __mul__ for each class you
have", which I don't think is what the OP asked for. First, nothing
in the original post said he wants the function to be used with
operator syntax, so __mul__ or other __xxx__ member functions should
be considered off-topic. Second, the OP asked for "specialization ",
and, as an example, asked for a way to cause the system "automatica lly
finds the right routine for [one type], and automatically call the
right code for [another type]". Your answer involves defining a
method for each type one creates, which (1) is not automatic, and (2)
requires one to modify the class, which is not what is expected by a
C++ programmer who are used to template functions and specialization.
Template specialization is a dispatch mechanism that is external to a
class. You probably mean "such mechanism is missing in Python". I'm
not quite sure that is correct, though.
Regards,
Isaac.
"Jive" <so*****@micros oft.com> wrote in message news:<su******* *************** @news.easynews. com>... When you define a function in Python, you are (in effect) defining many functions, just as Issac says. What the byte codes eventually do depends on type information that is acted on at runtime. C++ has a mechanism (templates) for acting on the type information similarly, but at compile time. However, a modern C++ compiler can do something beyond that. It can recognize special cases (which the programmer must code for) and use a different functional form (template) for the special case than for the generic case. Both STL and BGL use the mechanism extensively.
This doesn't look too hard. Untested, cause I don't have Python 2.4
yet:
def generic(genfunc ):
speclist = []
def wrapf(arg):
for (specfunc,typ) in speclist:
if isinstance(arg, typ):
return specfunc(arg)
return genfunc(arg)
wrapf.speclist = speclist
wrapf.func_name = genfunc.func_na me # isn't this ok in 2.4?
return wrapf
def specializes(gen f,typ):
def spd(specf):
genf.speclist.a ppend((specf,ty p))
return specf
return spd
# so my European friends don't get too upset
specialises = specializes
Then:
@generic
def some_function(a rg):
print "generic"
@specializes(so me_fuction,int)
def some_int_specif ic_function(arg ):
print "int-specific"
Whence calling some_function(' abc') prints "generic", and calling
some_function(1 23) prints "int-specific".
Obviously this example could use lots of improvement. It only works
for functions of one argument, and it could probably do better than a
linear search through the type-dispatch list. But it does show how it
can be done.
A couple things: I personally have a distaste for specializations ,
because of the easy possibily that what is supposed to be an
optimized, but equivalent, version of the generic function does
something different (intentionaly or not). IMO, that's much too stiff
a price for a premature optimization. They should rarely be used
outside the final stages of development. Or if you WANT the
specialization to behave differently, you ought to be using regular
old polymorphism.
Having said that, if you're going to use specializations , then the way
I did it above, by explicitly defining functions as generic and
specialized, is a much better way to do it than in C++ IMO. At least
then you get advance warning of any surprises. "Explicit is better
than implicit", 'n at. By not supporting implicit specializations , I
think Python's doing a good job.
--
CARL BANKS
[Isaac] [...] my post didn't make sense even to myself. So I cancel that post a minute after making it, although apparently my cancel posting didn't go as far as my response.
The comp.lang.pytho n newsgroup is mirrored by the py*********@pyt hon.org
mailing list, so canceling a message often won't work for the mailing list
subscribers. Your original message will already have been sent out by
email by the time your cancel message gets through.
--
Richie Hindle ri****@entrian. com
"Richie Hindle" <ri****@entrian .com> wrote in message
news:g5******** *************** *********@4ax.c om... [Isaac] [...] my post didn't make sense even to myself. So I cancel that post a minute after making it, although apparently my cancel posting didn't go as far as my response.
The comp.lang.pytho n newsgroup is mirrored by the py*********@pyt hon.org mailing list, so canceling a message often won't work for the mailing list subscribers. Your original message will already have been sent out by email by the time your cancel message gets through.
and python-list, along with 1000s of other technical mailing lists, is
mirrored as a news.gname.org newsgroup -- gmane.comp.pyth on.
I cancel about half the messages I start to write -- by hitting delete
instead of send ;-0.
Terry J. Reedy
Richie Hindle <ri****@entrian .com> wrote in message news:<ma******* *************** *************** *@python.org>.. . [Isaac] [...] my post didn't make sense even to myself. So I cancel that post a minute after making it, although apparently my cancel posting didn't go as far as my response.
The comp.lang.pytho n newsgroup is mirrored by the py*********@pyt hon.org mailing list, so canceling a message often won't work for the mailing list subscribers. Your original message will already have been sent out by email by the time your cancel message gets through.
Also, Google ignores cancel requests. If the article makes it to
Google's news server, which it likely will in a matter of seconds,
you're out of luck. Probably the only thing cancel's good for these
days is _immediately_ recalling an incomplete message that you
accidentally clicked post on.
--
CARL BANKS
Ian Bicking <ia**@colorstud y.com> wrote in message news:<ma******* *************** *************** *@python.org>.. . Jive Dadson wrote: If you have, for example, several data-types for matrices, how do you write code that will automatically find the right routine for quickly multiplying a vector by a diagonal matrix represented as a vector, and automatically call the right code for multiplying by a sparse matrix represented by some sparse coding, etc?
I haven't been following this thread, but Phillip Eby's recent implementation of generic functions should be mentioned if it hasn't been already:
http://dirtsimple.org/2004/11/generi...ve-landed.html
It might look something like this:
@dispatch.on('m 1', 'm2') def matrix_mul(m1, m2): "Multiply two matrices"
@matrix_mul.whe n(VectorMatrix, SparseMatrix) def matrix_mul_spar se(m1, m2): "Special implementation"
Actually, it would be more like:
@dispatch.gener ic()
def matrix_mul(m1, m2):
"""Multiply two matrices"""
@matrix_mul.whe n("m1 in VectorMatrix and m2 in SparseMatrix")
def matrix_mul_spar se(m1, m2):
"""Special implementation" ""
The 'dispatch.on()' decorator is for single-dispatch functions only.
If you want to dispatch on multiple arguments or on logical
conditions, you use 'dispatch.gener ic()'. The implementations for
single and multiple dispatch are completely separate, and use
different algorithms. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d1 = {'a':1}
>>> d2 = {'b':2}
>>> d3 = {'c':3}
|
by: Maxim Khesin |
last post by:
Hi,
being recently introduced to the joys of programming in a powerful
dynamic language (go snake!) I periodically rethink which parts of C++ I
still miss. One thing I really enjoy is the generics of C++ - i think
they are the single strong benefit of a strongly typed system. I was
wondering about the possibility of implementing STL-like algorithms in
Python and the one thing that I cannot think of doing without a kludge
is the object...
|
by: Fred Ma |
last post by:
Hello,
This is not a troll posting, and I've refrained from
asking because I've seen similar threads get all
nitter-nattery. But I really want to make a decision
on how best to invest my time. I'm not interested on
which language is better in *general*, just for my
purpose. My area of research is in CAD algorithms,
and I'm sensing the need to resort to something more
expedient than C++, bash scripting, or sed scripting.
|
by: Iwan van der Kleyn |
last post by:
Please ignore if you are allergic to ramblings :-)
Despite a puritan streak I've always tried to refrain from language wars
or syntax bickering; call it enforced pragmatism. That's the main reason
why I've liked Python: it's elegant and simple and still dynamic and
flexible. You could do worse for a clean and pragmatic language.
I do know my Smaltalk from my Common Lisp and my Ruby from my C#, so I
think I'm quite capable of escaping...
|
by: Davor |
last post by:
Is it possible to write purely procedural code in Python, or the OO
constructs in both language and supporting libraries have got so
embedded that it's impossible to avoid them? Also, is anyone aware of
any scripting language that could be considered as "Python minus OO
stuff"? (As you can see I'm completely new to Python and initially
believed it's a nice&simple scripting language before seeing all this
OO stuff that was added in over...
| |
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c */
#include <time.h>
|
by: metaperl |
last post by:
Hello, I am responsible for converting 30 loosey-goosey Perl scripts
into 30 well-documented easy to understand and use Python scripts.
No one has said anything in particular about how this should be done,
but the end product should be "professional" looking.
So, I'm looking for some books and suggestions which will help me write
high-quality scripts. I know about object-oriented programming and
application configuration and have spent...
|
by: metaperl |
last post by:
A Comparison of Python Class Objects and Init Files for Program
Configuration
=============================================================================
Terrence Brannon
bauhaus@metaperl.com
http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
|
by: WaterWalk |
last post by:
I've just read an article "Building Robust System" by Gerald Jay
Sussman. The article is here:
http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf
In it there is a footprint which says:
"Indeed, one often hears arguments against building exibility into an
engineered sys-
tem. For example, in the philosophy of the computer language Python it
is claimed:
\There should be one|and preferably only one|obvious...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |