Python handles globals badly.

Skybuck Flying skybuck2000 at hotmail.com
Fri Sep 11 04:23:26 EDT 2015


"Random832"  wrote in message 
news:mailman.242.1441758354.8327.python-list at python.org...

MRAB <python at mrabarnett.plus.com> writes:
> If you're allowed to specify both bounds, why would you be forbidden
> from negative ones?

"
It makes it non-obvious what value should be returned from e.g. search
methods that return a negative number on failure. .NET's IndexOf
function returns -1, but MaxValue if the array has a negative
bound. BinarySearch returns the complement of the nearest index to the
value you were searching for, which requires some gymnastics if you want
to make use of it for an array that has negative and positive bounds.
"

Yes pascal/Delphi allows negative bounds if I recall correctly, example:

var
    vIntegerArray : array[-10..10] of integer;

You have just given a very good reason why to not use negative values for 
return values/indications of success or failure.

In pascal I pretty much always try and use booleans to return success.

This is a smart thing to do... especially in pascal/Delphi, since one never 
knows when one is dealing with negative numbers having processing needs.

-1 could then cause problems/troubles/errors.

There is a drawback of using booleans which I suspect is the real reason why 
C programmers for example like to use -1 to indicate failure.

It's "speed/performance". Using a seperate boolean doubles memory 
requirement and might or might not require extra processing time from the 
CPU.

Delphi usually optimizes these booleans to be returned in EAX register... so 
it's a register based thing... if enough registers or so are available 
otherwise perhaps
some pushes/pops needed... not sure about that last thing. Whatever the case 
may be... I will assume for now... that nowadays the performance impact of 
using booleans
as return values is not that great ? Also I am not sure... but perhaps 
booleans allow safer/better procesing of boolean operations.

Not sure how -1 or if -1 could lead to problems with or/and statements... 
mixing C function results will also become problematic... sometimes C 
functions return 0 to indicate failure.

Sometimes 0 can even mean success.

So C is pretty inconsistent when it comes to return values.

Hence I believe Pascal/Delphi to be better/safer at this.

Bye,
  Skybuck. 




More information about the Python-list mailing list