The use of :

C

Chang LI

Some statements use : in the tail such as while x > 0: and def func():
What is the meaning and the usage of : in Python?
 
E

Erik Max Francis

Chang said:
Some statements use : in the tail such as while x > 0: and def func():
What is the meaning and the usage of : in Python?

It means something else follows in an indented block as part of the same
statement.
 
P

Paul Robson

Some statements use : in the tail such as while x > 0: and def func():
What is the meaning and the usage of : in Python?

It, together with the indent, identifies a block of code. Most languages
use either a terminating keyword like "WEND" "ENDIF" "NEXT" to identify
the end of a group of instructions, or alternatively a bracketing syntax
such as {} begin..end or $( $)

So these are roughly equivalent. The difference with the Python version
(the last one) is without the indentation it won't work - the indentation
does the job of WEND, { } and begin .. end

WHILE I > 0
PRINT I
I = I-1
WEND

while (i > 0)
{
printf("%d\n",i);
i = i-1;
}

while i > 0 do
begin
writeln i;
i := i - 1;
end;

while i > 0:
print i
i = i - 1

this code, unlike most other languages is different to the one above.
Changing the indentation actually changes what the code does.

while i > 0:
print i
i = i - 1

it is equivalent to C

while (i > 0)
{
printf("%d\n",i);
}
i = i-1;
 
C

Chang LI

Paul Robson said:
while i > 0 do
begin
writeln i;
i := i - 1;
end;

while i > 0:
print i
i = i - 1

So the : is similar to "begin" and the last space line is similar to
"end", right? How about

while i > 0 :
print i
i = i-1
 
T

Timo Virkkala

Chang said:
So the : is similar to "begin"
Correct

and the last space line is similar to "end", right?

Nope. You don't need a space line (you mean an empty line by that, right?),
you can just outdent one level and continue without any empty lines
(although in the interactive interpreter you need the empty line to end the
block on the first indentation level). So you could do:

i = 5
while i > 0:
print i
i = i-1
print "That's it"
> How about
while i > 0 :
print i
i = i-1

Works just fine. Other than the indentation, spacing is pretty much
irrelevant in Python.
 
R

Reinhold Birkenfeld

bruno said:
<please-someone-correct-me-if-i-am-wrong>
In fact, from a purely technical POV, the ':' could have been omitted
from the Python syntax, since indentation does the whole job of defining
blocks. It's only here for readability AFAIK.
</please-someone-correct-me-if-i-am-wrong>

Without the ':', single-line suites are impossible. If you allow only
multi-line suites, you're right.

Reinhold
 
B

bruno modulix

Timo Virkkala a écrit :

Not correct. You need both the ':' and the extra level of indentation on
next line, ie:

Python 2.3.3 (#2, Feb 17 2004, 11:45:40).... print "aha"
File "<stdin>", line 2
print "aha"
^
IndentationError: expected an indented block
Nope. You don't need a space line (you mean an empty line by that,
right?), you can just outdent one level and continue without any empty
lines (although in the interactive interpreter you need the empty line
to end the block on the first indentation level). So you could do:

i = 5
while i > 0:
print i
i = i-1
print "That's it"
.... print i
.... i -= 1
File "<stdin>", line 3
i = i-1
^
SyntaxError: invalid syntax.... print i
.... i -= 1
....
5
4
3
2
1
The level of indentation has to be the same within a block, and it has
to be *1* (*one*) extra-or-less level of indentation than the previous
block.

<please-someone-correct-me-if-i-am-wrong>
In fact, from a purely technical POV, the ':' could have been omitted
from the Python syntax, since indentation does the whole job of defining
blocks. It's only here for readability AFAIK.
</please-someone-correct-me-if-i-am-wrong>

Bruno
 
G

gabriele renzi

Reinhold Birkenfeld ha scritto:
Without the ':', single-line suites are impossible. If you allow only
multi-line suites, you're right.

are they really?
if <expression> <expressions>
and the likes would invho parse just fine
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

In the faq, http://www.python.org/doc/faq/gener...equired-for-the-if-while-def-class-statements,
it is stated that the colon is there "primarily to enhance readability
(one of the results of the experimental ABC language)." But can that
statement really be backed up? Has someone made a study or something?
I always thought the rule was "the less useless symbols, the higher
the readability." I.e:

if (a == b)
{
print a
}

is less readable than:

if a == b:
print a

Because it contains more non-significant symbols (, ), { and } that
"steal" the programmers attention. But consider

def f(x, y, z)
print x, y, z

to

def f(x, y, z):
print x, y, z

IMHO, the colon-less variant is more readable than the one with the colon.
 
J

Jeremy Bowers

are they really?
if <expression> <expressions>
and the likes would invho parse just fine

if "" "a" print "Hi"

Does that print Hi or not?

Dig deeper into Python grammar; make sure you know that statements and
expressions are different, and the counter-example above is based on
Python's string concatenation rules:

Python 2.3.4 (#1, Oct 26 2004, 20:13:42)
[GCC 3.4.2 (Gentoo Linux 3.4.2-r2, ssp-3.4.1-1, pie-8.7.6.5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.'ab'


The question is, what is the "<expression>", "" or "" "a"? The first is
false, the second true.

(At this point the natural response of many people is to start adding
rules and exceptions and "well, obviously"s... but none of them will beat
if <expression>:, or justify the removal of a colon if you compare back
to the original; that's the *real* competition. This is just a pre-emptive
point 'cause I've seen people do this sort of thing too often, it isn't
targetted directly at you, gabriele.)

A completely new grammar could certainly do away with it and there are
languages that can do that, but you almost certainly won't be able to get
there from here via incremental changes. (Forth is close but it is spelled
differently. Lisp natually encapsulates all expressions; I don't think it
quite captures the spirit of what I think you're getting at, but for the
Lisp definition of "expression", it works.)
 
J

Jeremy Bowers

I always thought the rule was "the less useless symbols, the higher the
readability." I.e:

There's a bit of circularity there, in that a symbol's use can *be* to
enhance readability. I'd certainly agree that instances where this holds
true is an exception rather than the rule, but it seems plausible to me
that Python is symbol-free enough for this to be a potential
justification.

The only punctuation you *need* is whitespace. See Forth (I don't know
if this is perfect but I'd bet the transform is simple), the HP RPN
calculator programming languages (IIRC, the only other punctuation is a
block delimiter, which would also be easy to transform away), or some
hacks I've cooked up for my own use where I minimized the token count
because I had to write the parser by hand and I was willing to trade a
little extra work to write the parsed stuff for an easier translation.
(Usually it is data, not programs, so while saying that's a "compilation"
is technically true it isn't what most people think of.) Take it to those
extremes and you'd see what I mean; extra symbols can help.

(Or, God help you, see the whitespace language for a demonstration that
all you need is whitespace, period.

http://compsoc.dur.ac.uk/whitespace/ )
 
P

Paul Robson

The only punctuation you *need* is whitespace. See Forth (I don't know
if this is perfect but I'd bet the transform is simple),

: Announce ." Forth has a fair bit of punctuation" ;
 
M

Marc 'BlackJack' Rintsch

<please-someone-correct-me-if-i-am-wrong>
In fact, from a purely technical POV, the ':' could have been omitted
from the Python syntax, since indentation does the whole job of defining
blocks. It's only here for readability AFAIK.
</please-someone-correct-me-if-i-am-wrong>

The ':' serves as a very good hint to "python aware" text editors to
automagically indent the next line after hitting return.

Ciao,
Marc 'BlackJack' Rintsch
 
N

Nick Coghlan

BJörn Lindqvist said:
Because it contains more non-significant symbols (, ), { and } that
"steal" the programmers attention. But consider

def f(x, y, z)
print x, y, z

to

def f(x, y, z):
print x, y, z

IMHO, the colon-less variant is more readable than the one with the colon.

Except that it is quite acceptable to do the following:

def f(x, y, z,
long_func_arg_name):
long_func_arg_name(x, y, z)


def f(x, y, z,
long_func_arg_name)
long_func_arg_name(x, y, z)

The colons do a decent job of flagging the beginning of suites, mainly because
of Python general lack of *other* punctuation (e.g. the colon would be entirely
ineffective at improving readability if every line ended with a semi-colon).

Cheers,
Nick.
 

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

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top