Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python is readable

Reply
Thread Tools

Re: Python is readable

 
 
Chris Angelico
Guest
Posts: n/a
 
      03-15-2012
On Thu, Mar 15, 2012 at 10:54 AM, Arnaud Delobelle <(E-Mail Removed)> wrote:
> I don't know this book and there may be a pedagogical reason for the
> implementation you quote, but pairwise_sum is probably better
> implemented in Python 3.X as:
>
> def pairwise_sum(list1, list2):
> * *return [x1 + x2 for x1, x2 in zip(list1, list2)]


Okay, here's something for debate.

Should the readability of a language be gauged on the basis of its
standard library, or should you be comparing actual code?

For instance, a quine in C can be fairly complex and messy, and it can
be unobvious what it's doing - but in HQ9+ it's easy. Is it fair to
compare on that basis, or should you actually implement the same /
equivalent code in each before judging?

Of course, that's all without getting into the question of what does
"readable" even mean. This has nothing to do with the eternal question
of whether it's more readable to use verbose English keywords or
cryptic symbols.

ChrisA
 
Reply With Quote
 
 
 
 
Rick Johnson
Guest
Posts: n/a
 
      03-15-2012
On Mar 14, 7:27*pm, Chris Angelico <(E-Mail Removed)> wrote:
> Okay, here's something for debate.
>
> Should the readability of a language be gauged on the basis of its
> standard library, or should you be comparing actual code?


I think the library matters greatly. Yes, one could argue that the
same functionality "could" be wrapped-up in the competing language,
but then, why has it not already been wrapped?

When comparing say, "language A" (WITHOUT a built-in print function)
and "Language B" (WITH a built-in print function), would you cry
unfairness when B wielded the built-in?

> [...]
> Of course, that's all without getting into the question of what does
> "readable" even mean.


One could get caught in an infinite loop of the "Chicken and the Egg"
paradox here. However, when we are talking about the Python
programming language "readable" simply means: "neophyte readable".
That is, "readable to someone with little or no experience with the
language". I think that has always been Python's philosophy from the
beginning (even all the way back to CP4E!).

> This has nothing to do with the eternal question
> of whether it's more readable to use verbose English keywords or
> cryptic symbols.


Again, for the case of Python, cryptic symbols are frowned upon. Of
course for the "hacker", cryptic symbols can save keystrokes and white
space -- but at the expense of "neophyte readability"!

> ChrisA


Thanks for reminding me of your name. I had almost forgotten who i
replied to
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      03-15-2012
Rick Johnson <(E-Mail Removed)> wrote:
> However, when we are talking about the Python
> programming language "readable" simply means: "neophyte readable".
> That is, "readable to someone with little or no experience with the
> language".


Nonsense. List comprehensions are not immediately obvious to new
Python users. The functionality of 'with' requires an understanding of
context managers. Python's readability has more to do with simplifying
code maintenance.

The idea that Python code has to be obvious to non-programmers is an
incorrect and dangerous one.


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      03-15-2012
On Thu, Mar 15, 2012 at 9:44 PM, Kiuhnm
<(E-Mail Removed)> wrote:
> Let's try that.
> Show me an example of "list comprehensions" and "with" (whatever they are).


I'll do a list comp, because they lend themselves well to one-liners.
what_am_i = '\n'.join(["%X\t%c"%(i,i) for i in range(12])

Okay, that one also uses printf formatting, which may be a smidge
obscure. Here's a simpler example:

what_am_i = [x*x for x in range(11)]

ChrisA
 
Reply With Quote
 
Thomas Rachel
Guest
Posts: n/a
 
      03-15-2012
Am 15.03.2012 11:44 schrieb Kiuhnm:

> Let's try that.
> Show me an example of "list comprehensions" and "with" (whatever they are).


with open("filename", "w") as f:
f.write(stuff)


with lock:
do_something_exclusively()


Thomas
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      03-15-2012
On Thu, Mar 15, 2012 at 10:27 PM, Kiuhnm
<(E-Mail Removed)> wrote:
> On 3/15/2012 11:50, Chris Angelico wrote:
>> I'll do a list comp, because they lend themselves well to one-liners.
>> what_am_i = '\n'.join(["%X\t%c"%(i,i) for i in range(12])

>
>
> A few conjectures:
> 1) '\n' is an object and join one of its methods;
> 2) [...] is a list comprehension;
> 3) that 'for' suggests that range isn't (or doesn't return) a list but an
> iterator;
> 4) points 2 and 3 suggest that [...] builds a list (or array?) by querying
> an iterator.
> 5) "%X\t%"(i,i) is probably equivalent to the C-like Perl's
> *sprintf("%X\t%c", i, i)
>
> So what_am_i is a simple ASCII table.


Correct. Actually, there's a few differences between Python 2 and 3;
in Py2, range() returns a list, but in Py3 an iterable object. But
'for' will happily iterate over a list.

>> Okay, that one also uses printf formatting, which may be a smidge
>> obscure. Here's a simpler example:
>>
>> what_am_i = [x*x for x in range(11)]

>
> what_am_i = 0, 1, 4, 9, ..., 100


Correct again.

> Your first example suggests that range(n) is a sequence iterator which
> returns, if queried n times,
> *0,...,n-1
> (which is a bit counterintuitive, IMHO).


It's a little odd, perhaps, if seen in a vacuum. But everything counts
from zero - list indices, etc - so it makes sense for range(len(lst))
to return indices valid for lst.

List comps are pretty readable if you know how programming languages
work. Python need not be readable by everyone and his grandmother, and
it does a fairly good job of being grokkable to someone who has a few
ranks in Coding. (Yeah, I'm a D&D nerd. )

ChrisA
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      03-15-2012
On Thu, Mar 15, 2012 at 10:59 PM, Kiuhnm
<(E-Mail Removed)> wrote:
> On 3/15/2012 12:47, Chris Angelico wrote:
>> It's a little odd, perhaps, if seen in a vacuum. But everything counts
>> from zero - list indices, etc - so it makes sense for range(len(lst))
>> to return indices valid for lst.

>
> Maybe range uses [...) intervals? So range(a,b) is a,a+1,a+2,...,b-1 and
> range(b) is just short-hand for range(0,b)?


Yup. It's amazing how accurate your conjectures are - it's almost like
you've been reading the docs! But yeah, that's pretty logical IMHO;
and having gotten used to [) intervals in many areas of computing,
I've come to find [] intervals disconcerting. Bible passages are
described as, for instance, John 14:5-7, which is a three-verse
passage (5, 6, 7), even though 7-5=2.

However, inclusive-inclusive intervals have the benefit that they
don't require the element "beyond the last" to be indexable. This is
important if you're working with something that takes up all of
addressable memory - going back to the IBM PCs on which I learned to
code, you could use one 64KB segment for an array, but then there's no
way for a 16-bit integer to indicate "past the end".

>> List comps are pretty readable if you know how programming languages
>> work. Python need not be readable by everyone and his grandmother, and
>> it does a fairly good job of being grokkable to someone who has a few
>> ranks in Coding. (Yeah, I'm a D&D nerd. )

>
> I like what I've seen so far.


Python has its problems, but it's a good language. I personally prefer
to delimit blocks of code with braces than with indentation, and I
also prefer explicit declaration of variables (yes, it's extra work,
but you can have infinitely nested scopes and easily-caught syntax
errors when you misspell one), but they're relatively minor. One of my
favorite aspects of Python is that *everything* is an object. There's
no magic syntax that gives you a piece of an object, or something
special about variables that contain this, that, or the other. A
literal list [like, this, one] can be used in exactly the same ways as
the name of a variable containing a list or a function call returning
a list - there is no difference. Oh how I yearn for that when working
in C++ or PHP!

ChrisA
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      03-15-2012
On Fri, Mar 16, 2012 at 12:16 AM, Ben Finney <(E-Mail Removed)> wrote:
> Chris Angelico <(E-Mail Removed)> writes:
>
>> On Thu, Mar 15, 2012 at 11:31 PM, Ben Finney <(E-Mail Removed)> wrote:
>> > Another good reason to advocate for proper typography. "John 14:5-7"
>> > indicates a range (because it uses U+2013 EN DASH), whereas "7-5"
>> > indicates subtraction (because it uses U+2212 MINUS SIGN). A hyphen
>> > ('-' U+002D) is inappropriate in either case.

>>
>> Heh. Yes, but when you're seeking the size of a range, you use
>> subtraction.

>
> Not the size of an *inclusive* range, such as a range indicated by use
> of an en dash


Yes you do, you just have to add one to it. You still use subtraction.

>> The fact that the en dash and minus sign are both often represented in
>> ASCII with a hyphen is pure coincidence.

>
> Hopefully, the fact that your quoting of my text munged the characters
> down to ASCII is also pure coincidence, and is soon to be corrected at
> your end? Or has your client program not joined the rest of us in the
> third millennium with Unicode?


It's gmail, and I don't know why it folded everything down. I've told
it to cast everything to text (to avoid the stupid look you sometimes
get with nested replies to HTML-formatted emails), and I guess it
decided to go ASCII. Your mail displayed just fine, it was something
in the reply mechanism.

ChrisA
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      03-15-2012
On 15/03/2012 11:48, Kiuhnm wrote:
> On 3/15/2012 12:14, Thomas Rachel wrote:
>> Am 15.03.2012 11:44 schrieb Kiuhnm:
>>
>>> Let's try that.
>>> Show me an example of "list comprehensions" and "with" (whatever they
>>> are).

>>
>> with open("filename", "w") as f:
>> f.write(stuff)

>
> Here f is created before executing the block and destroyed right after
> leaving the block. f's destructor will probably close the file handle.
>
>> with lock:
>> do_something_exclusively()

>
> It's clear what it does, but I don't know if that's special syntax.
> Maybe objects can have two special methods that are called respect. on
> entering and leaving the with-block.
> Or, more likely, lock creates an object which keeps the lock "acquired".
> The lock is released when we leave the block.
> So we could inspect the lock with
> with lock as l:
> inspect l...
> do_some.....
>
> BTW, aren't those ':' redundant?
>
> Kiuhnm


Nope.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("filename", "w") as f

File "<stdin>", line 1
with open("filename", "w") as f
^
SyntaxError: invalid syntax

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
Alec Taylor
Guest
Posts: n/a
 
      03-15-2012
On Fri, Mar 16, 2012 at 1:06 AM, Mark Lawrence <(E-Mail Removed)> wrote:
> Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> with open("filename", "w") as f

> *File "<stdin>", line 1
>
> * *with open("filename", "w") as f
> * * * * * * * * * * * * * * * * *^
> SyntaxError: invalid syntax
>
> --
> Cheers.
>
> Mark Lawrence.
>


Erred for me also; but under f:

Python 2.7.3rc1 (default, Feb 24 2012, 21:28:59) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("filename", "w") as f

File "<stdin>", line 1
with open("filename", "w") as f
^
SyntaxError: invalid syntax
 
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
Re: Number of languages known [was Re: Python is readable] - somewhatOT Nathan Rice Python 30 04-04-2012 07:07 AM
Re: Number of languages known [was Re: Python is readable] - somewhatOT Chris Angelico Python 4 04-03-2012 07:12 AM
Number of languages known [was Re: Python is readable] - somewhat OT Chris Angelico Python 15 03-24-2012 04:44 AM
Re: Python is readable Steven D'Aprano Python 3 03-16-2012 05:01 PM
Q: Is file readable by all users? Troll Perl 4 09-23-2003 05:43 PM



Advertisments