473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python STL?

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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.

Jul 18 '05 #1
11 3710
> 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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.


Hi Max,

Could you give a more specific example of what you miss?

-Dave

Jul 18 '05 #2
xam
std::sort - works on any well-ordered object collection (you just define
your own operator< if you need to sort based on some weird criteria)

"Dave Brueck" <da**@pythonapo crypha.com> wrote in message
news:ma******** *************** *************@p ython.org...
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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.


Hi Max,

Could you give a more specific example of what you miss?

-Dave

Jul 18 '05 #3
On Fri, 14 Nov 2003 21:07:16 GMT, "xam" <ma@Bell.com> wrote:
std::sort - works on any well-ordered object collection (you just define
your own operator< if you need to sort based on some weird criteria)

That particular one ALREADy exists in the standard sort(lt) funciton,
whose optional argument lt is any "less than" operator you care to
define.

"Dave Brueck" <da**@pythonapo crypha.com> wrote in message
news:ma******* *************** **************@ python.org...
> 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 comparsion that so much of STL relies on, which in turn
> relies on types and function(<) overloading. Is there a way to implement
> something similar in python (short of multimethods?). How does the
> python dictionary object allow arbitrary key types? Is in internally
> using the references' addresses to impose the tree ordering?
> thanks,
> max.


Hi Max,

Could you give a more specific example of what you miss?

-Dave


Jul 18 '05 #4

On Nov 14, 2003, at 1:07 PM, xam wrote:
std::sort - works on any well-ordered object collection (you just
define
your own operator< if you need to sort based on some weird criteria)


class Foo:
def __init__(self, bar):
self.bar = bar
def __cmp__(self, other):
return cmp(self.bar, other.bar)
def __repr__(self):
return 'Foo(%i)' % self.bar

myList = [Foo(1), Foo(7), Foo(3), Foo(8), Foo(-1755)]
myList.sort()
print myList

Yeilds:
[Foo(-1755), Foo(1), Foo(3), Foo(7), Foo(8)]

STL stands for Standard Template Library. With a dynamic type system
it's completely unnecessary -- you don't need a new type of list to
sort a new type of object.

Personally, I hate the STL because it adds code without adding
functionality and I avoid it at all costs when programming in C++.
It's also not as portable as it should be. I hope this doesn't spark a
big thread. Everyone programs in C++ for different reasons for
different requirements.
Jul 18 '05 #5
val
google with stl.py and enjoy it...
val

"Maxim Khesin" <ma*@cNvOiSsiPo AnMtech.com> wrote in message
news:0l******** *************@t wister.nyc.rr.c om...
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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.

Jul 18 '05 #6
Ron Levine:
[std::sort] ALREADy exists in the standard sort(lt) funciton,
whose optional argument lt is any "less than" operator you care to
define.


Not quite. STL splits algorithms and containers. Python's sort
is a method on native lists. Suppose I make my own list-like data
structure which implements __getitem__ and __setitem__
etc. as needed for the container aspect of a list. If I want to
sort those values I need to implement my own sort code,
with no ability to leverage the standard Python sort code.

Eg, suppose I had a fixed-width file where I can read/write
fields from the file and can replace entries, but cannot add
or delete fields.

class FileList:
def __init__(self, filename):
self.filename = filename
self.infile = open(filename, "rwb")
self._n = os.path.getsize (filename) / 40
def __len__(self): return self._n
def __getitem__(sel f, i):
if not (0 <= i < self._n):
raise IndexError(i)
return self.infile.see k(i*40).read(40 )
def __setitem__(sel f, i, s):
if not (0 <= i < self._n):
raise IndexError(i)
assert len(s) == 40
self.infile.see k(i*40).write(s )

The idea is that a 'std::sort' *algorithm* should be
able to use this list-like container even though there's
no implementation similarity between it and native lists.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #7
xam wrote:
"Dave Brueck" <da**@pythonapo crypha.com> wrote in message
news:ma******** *************** *************@p ython.org...
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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.


Hi Max,

Could you give a more specific example of what you miss?


std::sort - works on any well-ordered object collection (you just define
your own operator< if you need to sort based on some weird criteria)


Right - this is what I suspected: most of the C++ STL _functionality_ is
available in Python already. Not all of it, but at least what I find to be the
most useful stuff (and in general if it's not built-in it's "almost" built-in
in that it's trivial to implement, so much so that having a Python STL wouldn't
make sense).

For example, Python's standard sort() already works on object collections, and
you could define your own comparison method if you want.

-Dave
Jul 18 '05 #8
"Maxim Khesin" <ma*@cNvOiSsiPo AnMtech.com> wrote in message
news:0l******** *************@t wister.nyc.rr.c om...
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 comparsion that so much of STL relies on, which in turn
relies on types and function(<) overloading. Is there a way to implement
something similar in python (short of multimethods?). How does the
python dictionary object allow arbitrary key types? Is in internally
using the references' addresses to impose the tree ordering?
thanks,
max.


The STL (now part of the standard library) does with considerable
complexity* that which Python does very simply. That's because C++ templates
attempt to do generic programming without breaking the type system. Python
has no type system to break so generic program comes naturally.

--
Cy
http://home.rochester.rr.com/cyhome/

*if you don't agree with that, I will whack you one with my copy of Josuttis
:)
Jul 18 '05 #9

"Maxim Khesin" <ma*@cNvOiSsiPo AnMtech.com> wrote in message
news:0l******** *************@t wister.nyc.rr.c om...
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++
Then since Python programming generally *is* generic programming, it
is not surprising that you enjoy Python ;-).
- i think they are the single strong benefit of a strongly typed system.

And Python is more strongly typed than C/C++ (no type casts except
possibly for some user classes).
I was wondering about the possibility of implementing
STL-like algorithms in Python
From what I understand of STL, Python functions are STL or template
like unless one specificly adds type checks to inhibit the generality.
Python code works with any object with the needed interface and
behavior. Perhaps you have not grokked this because it is automatic
in Python.
and the one thing that I cannot think of doing without a kludge
is the object comparsion that so much of STL relies on, which in turn relies on types and function(<) overloading.
I do not understand this. Most builtin types come with working
comparison operations. User-defined classes can easily include them
also. I believe the builtin functions max and min are more
general/generic that what you can very easily write in C++.
Is there a way to implement something similar in python
Probably, but we need an example 'something'.
How does the python dictionary object allow arbitrary key types?
It accepts any hashable object by using the hash value.
Is in internally using the references' addresses
CPython uses object addresses, but that is an implementation detail
rather than part of the language definition. The C source is open and
pretty readable.
to impose the tree ordering?


There is no tree ordering as I understand the term.

Terry J. Reedy
Jul 18 '05 #10

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

Similar topics

0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10351
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,...
0
10096
tracyyun
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...
1
7638
isladogs
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...
0
6866
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();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.