Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46624

Re: How clean/elegant is Python's syntax?

References (3 earlier) <851ce96a-0223-42b0-8d99-902294c71f58@hc4g2000pbb.googlegroups.com> <CALwzidkvPJFWh1csV+sC6mFb9bZFaft_T5e+5E49eHucBTf-hQ@mail.gmail.com> <CAPTjJmqHE_Ctyn5dEL3TeXaan4_cF9JUWHi_hMb2nyafhpn5CQ@mail.gmail.com> <51A7AAC0.6080509@mrabarnett.plus.com> <CALwzid=kRrojg6B6uy_PYiYrcm9tsGbKZrSF7eMuGOt89UFJTQ@mail.gmail.com>
Date 2013-06-01 04:52 +1000
Subject Re: How clean/elegant is Python's syntax?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2501.1370026382.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jun 1, 2013 at 1:43 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, May 30, 2013 at 1:38 PM, MRAB <python@mrabarnett.plus.com> wrote:
>> And additional argument (pun not intended) for putting sep second is
>> that you can give it a default value:
>>
>>    def join(iterable, sep=""): return sep.join(iterable)
>
> One argument against the default is that it is specific to the str
> type.  If you then tried to use join with an iterable of bytes objects
> and the default sep argument, you would get a TypeError.  At least not
> having the default forces you to be explicit about which string type
> you're joining.

What about:

def join(iterable, sep=None):
    if sep is not None: return sep.join(iterable)
    iterable=iter(iterable)
    first = next(iterable)
    return first + type(first)().join(iterable)

Granted, it has some odd error messages if you pass it stuff that isn't strings:

>>> join([[1,2,3],[4,5,6]])
Traceback (most recent call last):
  File "<pyshell#241>", line 1, in <module>
    join([[1,2,3],[4,5,6]])
  File "<pyshell#235>", line 5, in join
    return first + type(first)().join(iterable)
AttributeError: 'list' object has no attribute 'join'

but you'd get that sort of thing anyway.

(NOTE: I am *not* advocating this. I just see it as a solution to one
particular objection.)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: How clean/elegant is Python's syntax? Ma Xiaojun <damage3025@gmail.com> - 2013-05-30 09:14 +0800
  Re: How clean/elegant is Python's syntax? rusi <rustompmody@gmail.com> - 2013-05-29 19:49 -0700
    Re: How clean/elegant is Python's syntax? Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-30 12:36 -0600
      Re: How clean/elegant is Python's syntax? rusi <rustompmody@gmail.com> - 2013-05-30 11:47 -0700
      Re: How clean/elegant is Python's syntax? John Ladasky <john_ladasky@sbcglobal.net> - 2013-05-30 15:01 -0700
    Re: How clean/elegant is Python's syntax? Chris Angelico <rosuav@gmail.com> - 2013-05-31 04:44 +1000
    Re: How clean/elegant is Python's syntax? Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-30 12:51 -0600
    Re: How clean/elegant is Python's syntax? MRAB <python@mrabarnett.plus.com> - 2013-05-30 20:38 +0100
    Re: How clean/elegant is Python's syntax? Chris Angelico <rosuav@gmail.com> - 2013-05-31 07:28 +1000
    Re: How clean/elegant is Python's syntax? Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-31 09:43 -0600
    Re: How clean/elegant is Python's syntax? Chris Angelico <rosuav@gmail.com> - 2013-06-01 04:52 +1000

csiph-web