Appeal for python developers

B

BOOGIEMAN

Please include "goto" command in future python realeses
I know that proffesional programers doesn't like to use it,
but for me as newbie it's too hard to get used replacing it
with "while", "def" or other commands
 
M

Michael Hoffman

BOOGIEMAN said:
Please include "goto" command in future python realeses

As has been said before, you can only use goto in Python if
you are using Python with line numbers:

http://groups-beta.google.com/group/comp.lang.python/msg/98264a0daa007c46
> I know that proffesional programers doesn't like to use it,
> but for me as newbie it's too hard to get used replacing it
> with "while", "def" or other commands

I suggest you learn. It shouldn't take you very long to get
used to it, and it will be much easier after you do so.

Trust us. ;)
 
?

=?ISO-8859-1?Q?Thomas_R=F6sner_aka_TRauMa?=

BOOGIEMAN said:
Please include "goto" command in future python realeses
I know that proffesional programers doesn't like to use it,
but for me as newbie it's too hard to get used replacing it
with "while", "def" or other commands

Technically, as a newbie you shouldn't know about GOTO at all. So you're
more a "Tainted by previous spaghetti code practices"-guy than newbie.

Have a look at "while ... else" and "break". Or use the april special
release of python featuring goto. But expect to be called names :).

Regards
T.
 
T

Torsten Bronger

Hallöchen!

BOOGIEMAN said:
Please include "goto" command in future python realeses I know
that proffesional programers doesn't like to use it, but for me as
newbie it's too hard to get used replacing it with "while", "def"
or other commands

Accordings to Stroustrup's C++ book, the only good reason for goto
statements in a language is to have it in computer-generated code.
I don't know whether programs generate Python, but I *think* that
even there "goto" can be avoided very easily.

Anyway, at our institute we control devices with HT Basic, and my
collegues have used lots of gotos, so I can understand that avoiding
them is somewhat inconvenient at the beginning. However, it's still
very easy to learn. Most gotos are disguised function calls, so
just copy the code in a "def". And loops can be translated to
"while"s almost trivially.

After a couple of days, it'll be even simpler than before.

Tschö,
Torsten.
 
D

Dave Reed

Technically, as a newbie you shouldn't know about GOTO at all. So you're
more a "Tainted by previous spaghetti code practices"-guy than newbie.


Or more likely a troll. Google for:

Boogieman yahoo troll

and you'll see this isn't the only place he/she does this sort of thing.

Please don't feed the trolls.

Dave
 
T

Torsten Bronger

Hallöchen!

Dave Reed said:
[...]

Technically, as a newbie you shouldn't know about GOTO at all. So
you're more a "Tainted by previous spaghetti code practices"-guy
than newbie.

Or more likely a troll. Google for:

Boogieman yahoo troll

and you'll see this isn't the only place he/she does this sort of
thing.

And this makes a troll?

Tschö,
Torsten.
 
A

Andrew Dalke

Torsten said:
Accordings to Stroustrup's C++ book, the only good reason for goto
statements in a language is to have it in computer-generated code.

I've needed goto statements when translating old code written
with gotos.
Most gotos are disguised function calls, so
just copy the code in a "def". And loops can be translated to
"while"s almost trivially.

True, but there are some that aren't easily translated. I
remember implementing code from Knuth. I think it was a
prime generation algorithm. It was MIX code that interwove
two loops and couldn't be easily untangled.

Since I just wanted to text it out I used C++ which had
both gotos and priority queues built in.

Andrew
(e-mail address removed)
 
B

beliavsky

Torsten said:
Hallöchen!



Accordings to Stroustrup's C++ book, the only good reason for goto
statements in a language is to have it in computer-generated code.
I don't know whether programs generate Python, but I *think* that
even there "goto" can be avoided very easily.

Goto is useful in breaking out of a nested loop and when there is a
clean-up section of a function that should be executed for various
error conditions.

In another newsgroup I once asked "who needs GOTO" and got some good
answers -- the thread can be found by Googling '(e-mail address removed)
"explicit GOTO"'. Goto's are less dangerous when they are in the
forward direction, to code appearing later.
 
G

Grant Edwards

Please include "goto" command in future python realeses
I know that proffesional programers doesn't like to use it,
but for me as newbie it's too hard to get used replacing it
with "while", "def" or other commands

Troll.
 
M

Michael Hoffman

Dave said:
Or more likely a troll. Google for:

Boogieman yahoo troll

and you'll see this isn't the only place he/she does this sort of thing.

I'm not convinced.
 
A

Andrew Dalke

beliavsky said:
Goto is useful in breaking out of a nested loop and when there is a
clean-up section of a function that should be executed for various
error conditions.

But much less useful in languages like Python which have exception
handling.

At rare times I've needed something like

for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
completely break out of the loop
else:
raise Exception("not found")
print "I have it!", i, j, k

There are four common solutions to this problem

1) use a goto statement to break out of the loop

for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
goto FOUND
else:
raise Exception("not found")

FOUND:
print "I have it!", i, j, k


2) wrap things inside of an exception

try:
for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
raise Found
else:
raise Exception("not found")
except Found:
pass
print "I have it!", i, j, k

3) have a mutiple-level break statement

for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
break 3
else:
raise Exception("not found")

print "I have it!", i, j, k

4) introduce a new function, possibly embedded

def _search():
for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
return i, j, k
raise Exception("not found")

i,j,k = _search()
print "I have it!", i, j, k

Both the exception and function definitions solutions can be
done now and I don't think the goto solution adds extra clarity
or elegance, so there's little gain at the expense of some
rather well known pitfalls.

In another newsgroup I once asked "who needs GOTO" and got some good
answers -- the thread can be found by Googling '(e-mail address removed)
"explicit GOTO"'. Goto's are less dangerous when they are in the
forward direction, to code appearing later.

I only found two google hits, both in a Fortran newsgroup. Other
posts by you suggest you often program in that language. Fortran
doesn't have exceptions, so gotos are the best solution for how
to do certain types of error handling. The same holds for C,
as you can tell by reading the Python source code.

Andrew
(e-mail address removed)
 
P

Paul McGuire

At the risk of beating this into the Pythonic ground, here is a
generator version which collapses the original nested loop into a
single loop, so that break works just fine:

..def getCombinations(*args):
.. if len(args) > 1:
.. for a0 in args[0]:
.. for remainder in getCombinations(*args[1:]):
.. yield [a0]+remainder
.. else:
.. for a0 in args[0]:
.. yield [a0]
..
..for i,j,k in getCombinations(xrange,yrange,zrange):
.. if lookup(i,j,k) == target:
.. print "Eureka!"
.. break
..else:
.. print "Rats! No match found."

Now that we have getCombinations in our toolkit, we can also do things
like:
..numbers = range(2)
..colors = ['red','green','blue','orange','white']
..sizes = ['S','M','L','XL','XXL']
..letters = "ABCDE"
..print [ c for c in getCombinations(numbers, colors) ]
..print [ c for c in getCombinations(numbers, colors, sizes) ]
..print [ c for c in getCombinations(letters,colors) ]
..print [ c for c in getCombinations(letters,letters) ] # take letters
two at a time
..print [ "".join(c) for c in getCombinations(letters,letters) ]

giving:

[[0, 'red'], [0, 'green'], [0, 'blue'], [0, 'orange'], [0, 'white'],
[1, 'red'], [1, 'green'], [1, 'blue'], [1, 'orange'], [1, 'white']]
[[0, 'red', 'S'], [0, 'red', 'M'], [0, 'red', 'L'], [0, 'red', 'XL'],
[0, 'red', 'XXL'], [0, 'green', 'S'], [0, 'green', 'M'], [0, 'green',
'L'], [0, 'green', 'XL'], [0, 'green', 'XXL'], [0, 'blue', 'S'], [0,
'blue', 'M'], [0, 'blue', 'L'], [0, 'blue', 'XL'], [0, 'blue', 'XXL'],
[0, 'orange', 'S'], [0, 'orange', 'M'], [0, 'orange', 'L'], [0,
'orange', 'XL'], [0, 'orange', 'XXL'], [0, 'white', 'S'], [0, 'white',
'M'], [0, 'white', 'L'], [0, 'white', 'XL'], [0, 'white', 'XXL'], [1,
'red', 'S'], [1, 'red', 'M'], [1, 'red', 'L'], [1, 'red', 'XL'], [1,
'red', 'XXL'], [1, 'green', 'S'], [1, 'green', 'M'], [1, 'green', 'L'],
[1, 'green', 'XL'], [1, 'green', 'XXL'], [1, 'blue', 'S'], [1, 'blue',
'M'], [1, 'blue', 'L'], [1, 'blue', 'XL'], [1, 'blue', 'XXL'], [1,
'orange', 'S'], [1, 'orange', 'M'], [1, 'orange', 'L'], [1, 'orange',
'XL'], [1, 'orange', 'XXL'], [1, 'white', 'S'], [1, 'white', 'M'], [1,
'white', 'L'], [1, 'white', 'XL'], [1, 'white', 'XXL']]
[['A', 'red'], ['A', 'green'], ['A', 'blue'], ['A', 'orange'], ['A',
'white'], ['B', 'red'], ['B', 'green'], ['B', 'blue'], ['B', 'orange'],
['B', 'white'], ['C', 'red'], ['C', 'green'], ['C', 'blue'], ['C',
'orange'], ['C', 'white']]
[['A', 'A'], ['A', 'B'], ['A', 'C'], ['B', 'A'], ['B', 'B'], ['B',
'C'], ['C', 'A'], ['C', 'B'], ['C', 'C']]
['AA', 'AB', 'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC']

Finally, these last two examples make me think of permutations as well
(in which order is significant - can't count both AB and BA). So here
is a brute force version of getPermutations, built on getCombinations,
but filtering previously reported duplicates:

..from sets import Set as set
..def getPermutations(*args):
.. prevs = []
.. for comb in getCombinations(*args):
.. thisComb = set(comb)
.. if not thisComb in prevs:
.. prevs.append(thisComb)
.. yield comb
..
..print [ c for c in getPermutations(letters,letters) ]
..print [ "".join(c) for c in getPermutations(letters,letters) ]

gives:
[['A', 'A'], ['A', 'B'], ['A', 'C'], ['B', 'B'], ['B', 'C'], ['C',
'C']]
['AA', 'AB', 'AC', 'BB', 'BC', 'CC']


-- Paul
 
L

Leif K-Brooks

Goto is useful [...] when there is a clean-up section of a function
that should be executed for various error conditions.

Like this?

def foo():
f = open('foo.txt')
try:
# do stuff with f
finally:
f.close()
 
D

Dennis Lee Bieber

"explicit GOTO"'. Goto's are less dangerous when they are in the
forward direction, to code appearing later.

UGH... That is the one direction I always avoid (in FORTRAN 77).
I use GOTO to get /back/ to some point that the reader of the code has
already seen. For the forward direction, I have found that I can usually
fit an IF block around the code that needs to be skipped.

Emulating a

repeat
code
until cond

is the place where I use a GOTO

label CONTINUE
code
IF (.NOT. cond) GOTO label

Even in this usage, the indentation and layout I use maps
directly to the more structured statements available in other languages.

--
 
D

Dennis Lee Bieber

for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
completely break out of the loop
else:
raise Exception("not found")
print "I have it!", i, j, k

There are four common solutions to this problem
said:
3) have a mutiple-level break statement

for i in xrange:
for j in yrange:
for k in zrange:
if lookup(i,j,k) == target:
break 3
else:
raise Exception("not found")

print "I have it!", i, j, k

I'd favor the Ada style... allowing labels on the loop
statement, and identifying the loop to break out of by label. Need some
sort of syntax to identify the label... For the example I'll use ! and
only label the outermost (since the code only needs to escape the
outermost)

outer! for i in xrange(..):
for j in yrange(..):
for k in zrange(..):
if lookup(i,j,k) == target:
break outer
else:
raise ...

I suppose the label could be extended for use with

for
while
if

constructs. One would be labeling the construct, and allow

break label

to move to the end of the construct. The label can only be reached from
within the construct (allowing labels to be reused on separate code
blocks, or even nested -- though that would be confusing). No
possibility of crossing structure boundaries.

first! for....


for ...
break first #INVALID, scope of first ended with other for loop


Hmmm I suspect for completeness, the label extension would have
to also apply to

continue <label>

--
 
S

Steven Bethard

Dennis said:
UGH... That is the one direction I always avoid (in FORTRAN 77).

Typical example of forward GOTOs in Python source:

static PyObject *
min_max(PyObject *args, PyObject *kwds, int op)
{
...
while (( item = PyIter_Next(it) )) {
/* get the value from the key function */
if (keyfunc != NULL) {
val = PyObject_CallFunctionObjArgs(
keyfunc, item, NULL);
if (val == NULL)
goto Fail_it_item;
}
...
else {
int cmp = PyObject_RichCompareBool(
val, maxval, op);
if (cmp < 0)
goto Fail_it_item_and_val;
else if (cmp > 0) {
...
}
}
}
if (PyErr_Occurred())
goto Fail_it;
...
return maxitem;

Fail_it_item_and_val:
Py_DECREF(val);
Fail_it_item:
Py_DECREF(item);
Fail_it:
Py_XDECREF(maxval);
Py_XDECREF(maxitem);
Py_DECREF(it);
return NULL;
}

Note that the GOTOs are basically there to take care of the appropriate
decref-ing if exceptions occur.

STeVe
 
A

Andrew Dalke

Paul said:
At the risk of beating this into the Pythonic ground, here is a
generator version which collapses the original nested loop into a
single loop, so that break works just fine:

Indeed. For some things I'm still in the pre-generator days of
Python. If I worked at it I think I could come up with an example
that wouldn't be so easy to turn into an iterator, but that would
be not only be beating it into the ground but doing a clog dance
on top.

Andrew
(e-mail address removed)
 
E

EP

Grant Edwards said:


I first checked my calendar to make sure it wasn't April 1, but some folks just can't wait, can they.

And another thought: all this use of alphanumeric characters --- aren't we obfuscating the pure binariness we should all know and love if we want to be one with the CPU?

010010100111010101110011011101000010000001101010011011110110101101101001011011100110011100100000011001100110111101101100011010110111001100100001
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top