Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Idea for joined() builtin

Reply
Thread Tools

Idea for joined() builtin

 
 
Rhamphoryncus
Guest
Posts: n/a
 
      08-16-2007
I know similar things have been argued before, but little things (like
the joined name implying a copy) can make a big difference. That and
I'm providing a simple implementation that works right now, so you
don't have to wait for it to ever become a builtin.

>>> joined([], [[1,2,3], [4,5,6]])

[1, 2, 3, 4, 5, 6]
>>> joined(' ', ['hello', 'world'])

'hello world'

def joined(sep, iterable):
if hasattr(sep, 'join'):
return sep.join(iterable) # string-like interface
else:
output = type(sep)() # Hopefully an empty container
for i in iterable:
output.extend(i) # list-like interface
output.extend(sep)
else:
if sep:
del output[-len(sep):]
return output

A little commentary on the intended usage. There are three common
ways to "combine" objects:

The first is adding numbers together. This may be accomplished with a
+b or sum([a, b]). All values affect the output, but their
individuality is lost; you cannot extract the original values[1].

The second is concatenation. This is usually done with list.extend,
''.join(), or with my joined function. It can also be done with a+b,
but the performance is substantially worse if done repeatedly. All
values affect the output and most of their individuality is retained;
given the original lengths of the subsequences, you can extract them
from the combined form.

The third is the "or" operation of a set or integer. This is done
with a|b. The values usually have some effect on the output, but as
redundant entries are removed, they lose a significant part of their
individuality.

The important thing to realize is that all of these different ways of
"combining" objects has different conceptual behaviour and different
performance characteristics. Get them confused or attempt to over-
generalize and you will be bitten.


[1] Math/crypto has some exceptions. Stop mentally poking holes in my
argument.

--
Adam Olsen, aka Rhamphoryncus

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM
Using group authentication with builtin Windows client srp336@getcoactive.com Cisco 1 01-23-2005 05:57 PM



Advertisments