By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

alternatives to making blocks like { } or other ??

P: n/a
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???
Jul 18 '05 #1
Share this Question
Share on Google+
31 Replies


P: n/a
Blocks in Python are defined not with {} but rather
with indention. You indent to start a block and
stop indenting when you wish to end it. Seems odd
at first, but turns out to be excellent way to
handle blocks (and to properly format you code).

HTH, Larry Bates

"CYBER" <usenet@zly_adres.com> wrote in message
news:c8**********@news.atman.pl...
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???

Jul 18 '05 #2

P: n/a
"Larry Bates" <lb****@swamisoft.com> wrote in message
news:FJ********************@comcast.com...
Blocks in Python are defined not with {} but rather
with indention. You indent to start a block and
stop indenting when you wish to end it. Seems odd
at first, but turns out to be excellent way to
handle blocks (and to properly format you code).

HTH, Larry Bates

"CYBER" <usenet@zly_adres.com> wrote in message
news:c8**********@news.atman.pl...
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???


.... and no religious wars on where the braces are supposed to go, HOORAY!!!

And I'll second Larry's comment - it takes about 10-20 minutes to get used
to, but then you just focus on indentation, and it seems *very* natural -
liberating, even!

-- Paul
Jul 18 '05 #3

P: n/a
CYBER wrote:
Is there any other way under python to create blocks ??
instead of
def sth(x):
return x
could i use something else like
def sth(x):
{
return x
}


Not without changing the language a lot. But just try the normal way for a
couple of days, you'll get used to it in no time.

--
Timo Virkkala
Jul 18 '05 #4

P: n/a
"Timo Virkkala" <a@a.invalid> wrote in message
news:Z1***************@read3.inet.fi...
CYBER wrote:
Is there any other way under python to create blocks ??
instead of
def sth(x):
return x
could i use something else like
def sth(x):
{
return x
}


Not without changing the language a lot. But just try the normal way for a
couple of days, you'll get used to it in no time.


Or you could try this, which works almost exactly as well:

def sth(x):
#
return x
#

<g,d&r>
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #5

P: n/a
On Tue, 18 May 2004 21:27:17 +0200, CYBER <usenet@zly_adres.com>
wrote:
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???

Of course there is another way!

Just use # in front of your favourite block separator.

I'm too lazy now to search the archives on who first suggested this,
but it works:

def something(x):
#{
return x
#}

However, as with any and all block separators, this can lead to holy
wars - is the above the 'one true way', or is it one of the following?
All are valid Python...

def something_else(x):
#{
return x
#}

def something_or_other(x):
#{
return x
#}

def something_completely_different(x): #(
return x
#}

--
Christopher
Jul 18 '05 #6

P: n/a
> Of course there is another way!

Just use # in front of your favourite block separator.

I'm too lazy now to search the archives on who first suggested this,
but it works:

def something(x):
#{
return x
#}


Thank you.

Jul 18 '05 #7

P: n/a

"CYBER" <usenet@zly_adres.com> wrote in message
news:c8**********@news.atman.pl...
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???


http://www.python.org/doc/Humor.html#parsing
Jul 18 '05 #8

P: n/a
On Wed, 19 May 2004 01:46:02 GMT, "Chad Thompson" <ch**@sabinasp.com>
wrote:

"CYBER" <usenet@zly_adres.com> wrote in message
news:c8**********@news.atman.pl...
Hello

Is there any other way under python to create blocks ??

instead of

def sth(x):
return x

could i use something else like

def sth(x):
{
return x
}
???


http://www.python.org/doc/Humor.html#parsing


Ah yes, that was the link, I'd completely forgotten about the Python
humour page!

Oh, and CYBER: my post was *meant* ironic, but Python is flexible
enough that it can be taken at face value - so glad to have been of
help ;-)

--
Christopher
Jul 18 '05 #9

P: n/a
On 2004-05-18, Paul McGuire <pt***@austin.rr._bogus_.com> wrote:
could i use something else like

def sth(x):
{
return x
}
... and no religious wars on where the braces are supposed to
go, HOORAY!!!
But as religious wars go, the great Python "tab" debate ranks
right up there.
And I'll second Larry's comment - it takes about 10-20 minutes
to get used to, but then you just focus on indentation, and it
seems *very* natural - liberating, even!


Especially if you've got an editor with a "python mode".

--
Grant Edwards grante Yow! I guess we can live
at on his POT FARM in HADES!!
visi.com
Jul 18 '05 #10

P: n/a
On 2004-05-18, CYBER <usenet@zly_adres.com> wrote:
Of course there is another way!

Just use # in front of your favourite block separator.

I'm too lazy now to search the archives on who first suggested this,
but it works:

def something(x):
#{
return x
#}


Thank you.


You realize that was a joke, right?

Please, please don't write code like that. You'll be shunned
just like the BASIC programmers who switched to C and pulled
crap like

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }

#define WHILE while (
#define DO ) {
#define DONE }

....

I once saw a very complete set of macros like that used to make
a C program look surprising like some brand or other of
structured BASIC. It was impressive in a sick sort of way. The
program didn't actually work right since the author was trying
to impliment a recursive descent parsing algorithm without
using recursion (he didn't know what recursion was, since BASIC
didn't have it), and was attempting to keep a "stack" in a user
space data structure without realizing that's what he was
doing.

--
Grant Edwards grante Yow! Now KEN is having
at a MENTAL CRISIS beacuse
visi.com his "R.V." PAYMENTS are
OVER-DUE!!
Jul 18 '05 #11

P: n/a

"Grant Edwards" <gr****@visi.com> wrote in message
news:sl*******************@grante.rivatek.com...
On 2004-05-18, CYBER <usenet@zly_adres.com> wrote:
Of course there is another way!

Just use # in front of your favourite block separator.

I'm too lazy now to search the archives on who first suggested this,
but it works:

def something(x):
#{
return x
#}


Thank you.


You realize that was a joke, right?

<snip>

Actually, it was worse than a joke, in that it looks like it works, but
really doesn't.

If you thought this was somehow magically doing grouping by reading the
commented-out braces, you could end up doing things like:

if condition: #{
do_something if condition is true
#} else: #{
do_something if condition is false
#}

which would have some surprising results (both true and false "paths" are
executed, because they are really the same path, the condition-is-true
path).

So, no, CYBER, this doesn't really work, although most cases will not
complain, and *usually* do what you want. But in general, don't even start
this habit, just use the language features as-designed.

-- Paul
Jul 18 '05 #12

P: n/a
Grant Edwards <gr****@visi.com> wrote in
news:sl*******************@grante.rivatek.com:
Please, please don't write code like that. You'll be shunned
just like the BASIC programmers who switched to C and pulled
crap like

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }

#define WHILE while (
#define DO ) {
#define DONE }


That's not even a very good set of macros.

You want:

#define IF if(((
#define THEN ))){

and then you can have something like:

#define AND ))&&((
#define OR )||(

for added fun and enjoyment. N.B. This cleverly changes the operator
priorities to match the language of your choice.

Safety Warning: The definitions of 'want' and 'cleverly' above may not
exactly match the usual dictionary definitions.
Jul 18 '05 #13

P: n/a
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message news:<sj********************@fe1.texas.rr.com>...
"Grant Edwards" <gr****@visi.com> wrote in message
news:sl*******************@grante.rivatek.com...
On 2004-05-18, CYBER <usenet@zly_adres.com> wrote:
> Of course there is another way!
>
> Just use # in front of your favourite block separator.
>
> I'm too lazy now to search the archives on who first suggested this,
> but it works:
>
> def something(x):
> #{
> return x
> #}

Thank you.


You realize that was a joke, right?

<snip>

Actually, it was worse than a joke, in that it looks like it works, but
really doesn't.

If you thought this was somehow magically doing grouping by reading the
commented-out braces, you could end up doing things like:

if condition: #{
do_something if condition is true
#} else: #{
do_something if condition is false
#}

which would have some surprising results (both true and false "paths" are
executed, because they are really the same path, the condition-is-true
path).

So, no, CYBER, this doesn't really work, although most cases will not
complain, and *usually* do what you want. But in general, don't even start
this habit, just use the language features as-designed.

-- Paul

I have to put my 2 cents in here. I love Python, but the one thing I
miss is using {} or something to enclose blocks. I edit in emacs
python-mode, and believe in indentation, but sometimes I'll
inadvertently change a line of code's indentation and it throws the
logic off. I found a bug in one of my programs recently where a line
of code should have been outside of an 'if', but wasn't because I hit
TAB one too many times. A {} block would've caught that. I know that's
just being careless, but I need all the help I can get!

One thing I have done is to put a '#' at the end of the block so I
know not to indent the next line when adding code.
Jul 18 '05 #14

P: n/a
Tim Williams wrote:
I have to put my 2 cents in here. I love Python, but the one thing I
miss is using {} or something to enclose blocks. I edit in emacs
python-mode, and believe in indentation, but sometimes I'll
inadvertently change a line of code's indentation and it throws the
logic off. I found a bug in one of my programs recently where a line
of code should have been outside of an 'if', but wasn't because I hit
TAB one too many times. A {} block would've caught that. I know that's
just being careless, but I need all the help I can get!


And if you came back a few months later, wouldn't you have been confused
about what your code was doing? The beauty of Python's indentation is
that the code does what you'll expect from looking at it.
Jul 18 '05 #15

P: n/a
Grant Edwards wrote:
(he didn't know what recursion was, since BASIC
didn't have it)


That's not quite true. Recursive GOSUBs worked fine in
most versions of BASIC. There just weren't any local
variables or parameter passing, making recursion
somewhat awkward to use...

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #16

P: n/a
Tim Williams wrote:
I found a bug in one of my programs recently where a line
of code should have been outside of an 'if', but wasn't because I hit
TAB one too many times.


And for some reason it wasn't obvious from looking
at the indentation?

In a brace-delimited language, you can get the
opposite problem, which is much worse: the code
*looks* right judging by the indentation, but it
doesn't match the brace structure.

Python is a WYSIWYG language!

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #17

P: n/a
Grant Edwards <gr****@visi.com> wrote in message news:<sl*******************@grante.rivatek.com>...
On 2004-05-18, CYBER <usenet@zly_adres.com> wrote:
Of course there is another way!

Just use # in front of your favourite block separator.
.... You realize that was a joke, right?

Please, please don't write code like that. You'll be shunned
just like the BASIC programmers who switched to C and pulled
crap like

#define IF if (
#define THEN ) {
#define ELSE } else {
#define ENDIF }
Also,

#define ELSEIF } else if {

because using "ELSE IF" gives you unbalanced braces.
#define WHILE while (
#define DO ) {
#define DONE }


I don't remember BASIC ever using the syntax "WHILE cond DO ... DONE";
you've probably got it mixed up with another language.

"WHILE cond ... WEND" would be more accurate, but then it's harder to
deal with the ")" on the first line.

Also, you might want to support the QBasic DO...LOOP syntax, which had
four forms:

' 1. Pretest loop (while), positive condition
DO WHILE cond : statements : LOOP
' 2. Pretest loop (while), negative condition
DO UNTIL cond : statements : LOOP
' 3. Posttest loop (do...while), positive condition
DO : statements : LOOP WHILE cond
' 4. Posttest loop (do...while), negative condition
DO : statements : LOOP UNTIL cond

I doubt it's even possible to #define DO, LOOP, WHILE, and UNTIL to
handle all of them.
Jul 18 '05 #18

P: n/a
Greg Ewing <gr**@cosc.canterbury.ac.nz> wrote in message news:<2h************@uni-berlin.de>...
Tim Williams wrote:
I found a bug in one of my programs recently where a line
of code should have been outside of an 'if', but wasn't because I hit
TAB one too many times.
And for some reason it wasn't obvious from looking
at the indentation?


Not right away. It wasn't until I noticed things started running
differently that I ran it in the debugger and saw my error. I had the
offending line in the right place at one point, but after adding some
code, I mistakenly indented that one line when I was indenting the
new code. (I think that's what happened anyway - it's been awhile.)

I don't know about anyone else, but I've spent more than enough time
looking over and over at pieces of my code where a bug existed, not
finding the error sooner because I "knew" this piece of code was
right. Maybe this just says something about my programming ability.

In a brace-delimited language, you can get the
opposite problem, which is much worse: the code
*looks* right judging by the indentation, but it
doesn't match the brace structure.

Python is a WYSIWYG language!


But at least there, I can turn on paren-match mode in emacs and see
what code is in the block. If it's indented inconsistenly, then I can
fix how it looks, but I'm not changing the logic with whitespace.

Enough of this thread. I'll agree to disagree on this.
Jul 18 '05 #19

P: n/a
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message news:<sj********************@fe1.texas.rr.com>...
"Grant Edwards" <gr****@visi.com> wrote in message
news:sl*******************@grante.rivatek.com...
On 2004-05-18, CYBER <usenet@zly_adres.com> wrote:
> Of course there is another way!
>
> Just use # in front of your favourite block separator.
>
> I'm too lazy now to search the archives on who first suggested this,
> but it works:
>
> def something(x):
> #{
> return x
> #}

Thank you.


You realize that was a joke, right?


Joke? On my previous job my boss included smthng like that to local
code standard. everybody were supposed to do that.

if python would have compatibility mode for braces, please...

otherwise perl is everywhere because of braces and most people were taking
classes in C or Java or C++. that the only reason besides people like dollar
signs :)

i do really suffer. i have to use Perl and i know that i have no chance
to convince my new boss (very smart guy) to switch to Python because of
braces. that means wasted time on debugging perl crap code and listening
about Perl 6 future might.

Please, Python writers, do provide braces as compatibility mode.

Give the chance to the world!

Andrei
Jul 18 '05 #20

P: n/a
Andrei Smirnov wrote:
i do really suffer. i have to use Perl and i know that i have no chance
to convince my new boss (very smart guy) to switch to Python because of
braces.


Clearly your new boss is *not* very smart, or is insane.
No sane, intelligent person would reject Python *solely* because it
doesn't use braces. Maybe that plus some other reasons, but not as the
*only* reason...

-Peter
Jul 18 '05 #21

P: n/a
as****************@yahoo.com (Andrei Smirnov) wrote in
news:9a*************************@posting.google.co m:

Please, Python writers, do provide braces as compatibility mode.

Give the chance to the world!


The world seems to be switching to Python quite happily despite (or
possibly because of) the lack of braces.

If you must do this, then Python comes with a script to insert/remove #end
directives. See Tools/Scripts/pindent.py. Use "pindent.py -c" whenever your
boss wants to review the code, and "pindent.py -d" immediately afterwards.
Jul 18 '05 #22

P: n/a
On 2004-05-20, Andrei Smirnov <as****************@yahoo.com> wrote:
> I'm too lazy now to search the archives on who first suggested this,
> but it works:
>
> def something(x):
> #{
> return x
> #}

Thank you.

You realize that was a joke, right?


Joke? On my previous job my boss included smthng like that to local
code standard. everybody were supposed to do that.


The man's a loon. Or maybe just dim. Probably both.

Or possibly just humor-impared and took a sarcastic Usenet
posting seriously...

--
Grant Edwards grante Yow! BELA LUGOSI is my
at co-pilot...
visi.com
Jul 18 '05 #23

P: n/a
as****************@yahoo.com (Andrei Smirnov) wrote:
i do really suffer. i have to use Perl and i know that i have no chance
to convince my new boss (very smart guy) to switch to Python because of
braces. that means wasted time on debugging perl crap code and listening
about Perl 6 future might.

Please, Python writers, do provide braces as compatibility mode.


Asking for braces in Python is like trying to teach a pig to sing; it
won't work and it annoys the pig.

For better or worse, Python uses indentation for statement grouping.
There are more or less three ways to deal with this:

1) Decide you like the idea and use the language.

2) Decide you don't like the idea, but find enough else worthwhile in
the language that you get past your dislike of indentation and use the
language anyway.

3) Decide the indentation issue is so onerous to you that you can't get
past it and don't use the language.

Any of those are valid. Personally, I fall into camp #2, and think
people in camp #3 are being obstinate fools, but any of the above is
preferable to trying to get the language maintainers to add braces to
the language. It just ain't going to happen.

On the other hand, anybody is free to download the source code to the
system and modify it in any way they see fit. If people like it,
they'll adopt it. Lots of people have come up with variants, add-ons,
or outgrowths of the core language: Jython, NumPy, Prothon, Pyrex,
Psyco, Parrot, etc. Just in the last few days has yet another one
called Pythong been mentioned, although details are still a bit sketchy
on that :-)
Jul 18 '05 #24

P: n/a
>>>>> "Andrei" == Andrei Smirnov <as****************@yahoo.com> writes:

Andrei> Please, Python writers, do provide braces as compatibility mode.

Andrei> Give the chance to the world!

If you think that the designer who is proud of eliminating the braces in the
language is any easier to change than your boss, you're deadly mistaken
about the reasons why people are writing open-source software.

Regards,
Isaac.
Jul 18 '05 #25

P: n/a
Roy Smith <ro*@panix.com> wrote in news:roy-
66*******************@reader2.panix.com:
Just in the last few days has yet another one
called Pythong been mentioned, although details are still a bit sketchy
on that :-)


Pythong with braces. What a mental image. :-)
Jul 18 '05 #26

P: n/a
In article <Xn***************************@127.0.0.1>,
Duncan Booth <me@privacy.net> wrote:
Roy Smith <ro*@panix.com> wrote in news:roy-
66*******************@reader2.panix.com:
Just in the last few days has yet another one
called Pythong been mentioned, although details are still a bit sketchy
on that :-)


Pythong with braces. What a mental image. :-)


Now, imagine that along with a "My nibbles explode with desire" T-shirt.
Jul 18 '05 #27

P: n/a

"Andrei Smirnov" <as****************@yahoo.com> wrote in message
news:9a*************************@posting.google.co m...
Please, Python writers, do provide braces as compatibility mode.


The Python developers are all volunteers working on the parts of the
language they find interesting or at least tolerable. There plates are
completely full with a backlog of requested bugfixes and feature or module
additions. If *you* want something different (Braython?), you can get
together with the other brace advocates and either do it yourselves or hire
someone. The source code is available and the license permissive.

Terry J. Reedy


Jul 18 '05 #28

P: n/a
In article <9a*************************@posting.google.com> ,
as****************@yahoo.com (Andrei Smirnov) wrote:
Please, Python writers, do provide braces as compatibility mode.

Give the chance to the world!


The idea is a serious violation of the "One obvious way
to do it" rule. The thread has already dealt with the
confusion created by two obvious ways to do it: when the
compiler uses {} and the humans use indentation.

The best solution would be for somebody to volunteer to
write a program that would take Python source, marked up
with block brackets (perhaps '##{' and '##}', perhaps not)
and create properly indented Python. If you volunteered for
this you could also throw in the facility to mark up
correctly indented Python source that you wanted to go on to
modify. I doubt the facility is going to show up in Python
any other way anytime soon. Python 13.0 maybe.

Regards. Mel.
Jul 18 '05 #29

P: n/a
>>>>> "Mel" == Mel Wilson <mw*****@the-wire.com> writes:

Mel> The best solution would be for somebody to volunteer to
Mel> write a program that would take Python source, marked up
Mel> with block brackets (perhaps '##{' and '##}', perhaps not)
Mel> and create properly indented Python. If you volunteered for

It already exists in Python - it's the Tools/Scripts/pindent.py
script.

Here's the copy-pastage of the way code is written, for those who
don't care enough to check:

def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b > a: a = a-1
# end if
else:
print 'oops!'
# end if
# end def foobar

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #30

P: n/a
Peter Hansen <pe***@engcorp.com> wrote in message news:<rf********************@powergate.ca>...
Andrei Smirnov wrote:
i do really suffer. i have to use Perl and i know that i have no chance
to convince my new boss (very smart guy) to switch to Python because of
braces.
Clearly your new boss is *not* very smart, or is insane.


there are many smart people who are not familiar with any aspect of
this
discussion (and other way around). so, don't make judgement based on
preferences.
No sane, intelligent person would reject Python *solely* because it
doesn't use braces. Maybe that plus some other reasons, but not as the
*only* reason...
sure it is not only reason. as usual the only reason: there are
a lot of code we wrote on perl and ...

well, there is such thing as initial impression. and python initial
impression
for (smart or stupid) guy who was using C for years is like swap brake
and gas
pedal in the car. why should anybody do this? the real fact is that
C-look is everywhere and it is affecting decision-making process in
this way.

BTW, people using computer languages not only for full-time job
(like myself till recently) or to build stupid websites but also to do
some usefull things :)

i took me about two days to get used to python indentation. and it is
actually
not very hard to convince your friend to do it. but things
are very different in any industry and when you interacting with your
boss.

i will be surprised if this arguments will convince anybody here but i
need
some relief before i get back to perl (that's part of my job).

-Peter


Andrei
Jul 18 '05 #31

P: n/a
>>>>> "Andrei" == Andrei Smirnov <as****************@yahoo.com> writes:
Andrei Smirnov wrote:
> i do really suffer. i have to use Perl and i know that i have
> no chance to convince my new boss (very smart guy) to switch
> to Python because of braces.


Clearly your new boss is *not* very smart, or is insane.


Andrei> there are many smart people who are not familiar with any
Andrei> aspect of this discussion (and other way around). so,
Andrei> don't make judgement based on preferences.

He's probably not stupid, just prejudiced. Many people in tech
are. You just need persistence in convincing, and you may need to
demonstrate how productive/readable Python is. Never give up - even
the most stubborn will see that Python absolutely kills Perl, given
enough exposure. Just write something and show him the code. Make it
brief and elegant. The indentation issue will fall by the wayside.

Andrei> well, there is such thing as initial impression. and
Andrei> python initial impression for (smart or stupid) guy who
Andrei> was using C for years is like swap brake and gas pedal in
Andrei> the car. why should anybody do this? the real fact is that
Andrei> C-look is everywhere and it is affecting decision-making
Andrei> process in this way.

It might very well be affecting the decision making process, but so
should you. It's not necessary to sit silently while other people make
the decisions - programmers should complain loudly when the decisions
made are stupid, especially if they concern them.

Andrei> not very hard to convince your friend to do it. but things
Andrei> are very different in any industry and when you
Andrei> interacting with your boss.

The boss is a person like anyone else, with a pair of ears and at
least half a brain.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #32

This discussion thread is closed

Replies have been disabled for this discussion.