Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > code review

Reply
Thread Tools

code review

 
 
Roy Smith
Guest
Posts: n/a
 
      07-13-2012
On Friday, July 6, 2012 9:58:10 AM UTC-4, Steven D'Aprano wrote:
> (Sadly, when I say "we" I mean
> collectively. Many language designers, and programmers, don't have the
> foggiest clue as to what makes a good clean design. Hence C++ and PHP.)


I'm not going to defend C++, but to be fair, a major driver of the design is that it had to be plug-compatible with C. From that you're stuck with the preprocessor and pointers. Much goes downhill when you start from there.

PHP, yeah, that's just charlie-foxtrot from start to finish.
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      07-13-2012
On Sat, Jul 14, 2012 at 1:04 AM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> Actually, no. Is True less than False, or is it greater? In boolean
> algebra, the question has no answer. It is only an implementation detail
> of Python that chooses False < True.


Maybe in boolean algebra, but in code, it's handy to have sortable
bools. In SQL, for instance, I can use a boolean in an ORDER BY,
perhaps followed by another criterion, and it's effectively sorting by
"1 if some_boolean else 0" or in C notation "some_boolean ? 0 : 1"

ChrisA
 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      07-13-2012
On Jul 13, 8:36*pm, Chris Angelico <(E-Mail Removed)> wrote:
> On Sat, Jul 14, 2012 at 1:04 AM, Steven D'Aprano
>
> <(E-Mail Removed)> wrote:
> > Actually, no. Is True less than False, or is it greater? In boolean
> > algebra, the question has no answer. It is only an implementation detail
> > of Python that chooses False < True.

>
> Maybe in boolean algebra, but in code, it's handy to have sortable
> bools. In SQL, for instance, I can use a boolean in an ORDER BY,
> perhaps followed by another criterion, and it's effectively sorting by
> "1 if some_boolean else 0" or in C notation "some_boolean ? 0 : 1"
>
> ChrisA


Actually a boolean algebra is a lattice with some additional
properties
A lattice is a poset (partially ordered set) with suprema and infimas
And so there is one natural order relation on any boolean algebra
which may be defined as
a ≤ b iff a ⋀ b = a

tl;dr version: In a boolean algebra, False is bottom and True is top
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      07-13-2012
On 13 Jul 2012 15:04:21 GMT, Steven D'Aprano
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

> Actually, no. Is True less than False, or is it greater? In boolean
> algebra, the question has no answer. It is only an implementation detail
> of Python that chooses False < True.
>

And for the real fun... VMS F77 (if not the VAX/Alpha architecture
itself) basically used lsb making 1, 3, 5... True; and 0, 2, 4... False
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-13-2012

>From now on, for each operator I would have to remember wether it
> is a supposedly comparison operator or not.


I believe the following rule is true: if a op b is True or False raises,
then op is a potentially chained comparison operation. They are (not)
equal (and (not) is), the 4 order comparisons, and (not) in. 'in' should
be the only surprise and most confusing.
>>> 1 < 3 in {3,4}

True
>>> 3 in {3,4} < {4}

False

'and' and 'or' are not included because they do not always return a
bool, and indeed, they are not binary operators in the usual sense
because of short-circuiting.

--
Terry Jan Reedy



 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      07-14-2012
On Fri, Jul 13, 2012 at 5:09 PM, Terry Reedy <(E-Mail Removed)> wrote:
>
>> From now on, for each operator I would have to remember wether it
>> is a supposedly comparison operator or not.

>
>
> I believe the following rule is true: if a op b is True or False raises,


I don't follow. Raises what?

> then op is a potentially chained comparison operation. They are (not) equal
> (and (not) is), the 4 order comparisons, and (not) in. 'in' should be the
> only surprise and most confusing.
>>>> 1 < 3 in {3,4}

> True
>>>> 3 in {3,4} < {4}

> False
>
> 'and' and 'or' are not included because they do not always return a bool,
> and indeed, they are not binary operators in the usual sense because of
> short-circuiting.


The only one of those operators that can be said to always return a
bool is "is (not)". The others (apart from "and" and "or") all can be
overloaded to return anything you want (for example, sqlalchemy
overloads them to return expression objects that are later compiled
into SQL), and chaining still occurs regardless of the types they are
applied to.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-14-2012
On 7/14/2012 5:26 AM, Ian Kelly wrote:
> On Fri, Jul 13, 2012 at 5:09 PM, Terry Reedy <(E-Mail Removed)> wrote:


>> I believe the following rule is true: if a op b is True or False raises,


Sorry, left out 'or' in 'or raises'
>
> I don't follow. Raises what?


an Exception.

>> then op is a potentially chained comparison operation. They are (not) equal
>> (and (not) is), the 4 order comparisons, and (not) in. 'in' should be the
>> only surprise and most confusing.
>>>>> 1 < 3 in {3,4}

>> True
>>>>> 3 in {3,4} < {4}

>> False
>>
>> 'and' and 'or' are not included because they do not always return a bool,
>> and indeed, they are not binary operators in the usual sense because of
>> short-circuiting.

>
> The only one of those operators that can be said to always return a
> bool is "is (not)". The others (apart from "and" and "or") all can be
> overloaded to return anything you want


OK, add 'by default, without over-rides .
or 'for builtin classes'.

Python's flexibility makes it really hard to make any general statement.
In my book-in-progress, I am currently resorting to saying "In Python*,
...." whenever I know and remember that it is possibly to over-ride the
customary behavior described in '...'.

--
Terry Jan Reedy



 
Reply With Quote
 
Albert van der Horst
Guest
Posts: n/a
 
      07-17-2012
In article <XnsA0927750022F4duncanbooth@127.0.0.1>,
Duncan Booth <(E-Mail Removed)> wrote:
>Steven D'Aprano <(E-Mail Removed)> wrote:
>
>> On Fri, 13 Jul 2012 12:30:47 +0000, Albert van der Horst wrote:
>>> The worst of is, of course, = for assignment instead of := . This is
>>> a convention that Python follows, to my dismay.

>>
>> *shrug*
>>
>> The worst is to use = for both equality and assignment, like some
>> BASICs. At least Python does not allow assignment as an expression, so
>> you can't make the typical C error of:
>>
>> if x = y: do_something() # oops meant x == y
>>

>Technically of course Python doesn't have assignment, it just binds names.
>
>Albert raised the subject of Algol 68 which if I remember correctly used :=
>for assignment and = to bind names (although unlike Python you couldn't
>then re-bind the name to another object in the same scope).


Algol 68 is very particular about this indeed.
For instance they have a whole theory behind
real x;
x := 17.;

This is considered an abbreviation of
ref real x = loc real;
x:= 17;

So x is a reference bound to a freshly generated local real.
You see = and that means you can't ever break that relationship.
On the left side of a := it is required to have a ref something.
Because that generates a pretty clear context, you have considerable
leeway on the right side, that is cast into the something.

real x= 18.;
x := 15.;
is right out.

If you want to rebind something you need a ref which is really
a ref ref. Then you can only switch bindings between different
types. So it is totally different from Python.

I never thought about = in python to mean binding to set it
apart from the Pascal := , instead of being a c-ism.

Still my mathematical mind is bothered about the sequence
( legal in FORTRAN , C Python )
X = 1 <separator>
X = 2

Groetjes Albert

>--
>Duncan Booth http://kupuguy.blogspot.com


Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

 
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
What is code review? (Java code review) www Java 51 05-15-2007 01:10 PM
Secure Python code - volunteers for code review? andrew blah Python 6 10-17-2004 01:17 AM
Re: Secure Python code - volunteers for code review? Josiah Carlson Python 1 10-13-2004 03:05 PM
Code write \ code review productivity Volodymyr Sadovyy Java 8 04-25-2004 03:30 AM
Code review of cross platform code sample Otto Wyss C++ 5 09-07-2003 02:06 PM



Advertisments