Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Indentation for code readability

Reply
Thread Tools

Indentation for code readability

 
 
DE
Guest
Posts: n/a
 
      03-30-2007
Hello,

Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherStuff()
}
popMatrix();
}
popMatrix();

The curly brackets have no functional meaning but increase the
readability significantly. I want to be able to do the same thing in
python. Since curly brackets are not available and indenting without
an if or while conditional doesn't work, I have started to question if
this is possible in python at all.

Any ideas ?

MDE

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Thomas_Kr=FCger?=
Guest
Posts: n/a
 
      03-30-2007
DE schrieb:
> Hello,
>
> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();
> }
> popMatrix();
>
> The curly brackets have no functional meaning but increase the
> readability significantly. I want to be able to do the same thing in
> python. Since curly brackets are not available and indenting without
> an if or while conditional doesn't work, I have started to question if
> this is possible in python at all.
>
> Any ideas ?


I've been thinking about that for some minutes now and I have doubts
that it will increase the readability. Maybe for you as you invented
that style but not for others.
There are a few standards for formatting C code and even this few have
cause many discussions between developers.
Python has one convention (defined in PEP and the deeper you dive
into the language the more you will like it.
BTW: having one way to do it is one of the main ideas of Python's
philosophy.

Thomas
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      03-30-2007
On Mar 30, 7:04 pm, "DE" <devrim.er...@gmail.com> wrote:
> Hello,
>
> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();}
>
> popMatrix();
>
> The curly brackets have no functional meaning but increase the
> readability significantly. I want to be able to do the same thing in
> python. Since curly brackets are not available and indenting without
> an if or while conditional doesn't work, I have started to question if
> this is possible in python at all.
>
> Any ideas ?
>


You *can* use round brackets and/or square brackets. E.g.

def pushMatrix():
drawStuff()
pushMatrix()
(
drawSomeOtherStuff()
)
[
drawEvenMoreStuff()
]
popMatrix()

Whether you *should* do that is a different question ... It's a bit
like an English definition of a Scottish gentleman: one who can play
the bagpipes, but doesn't

HTH,
John

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      03-30-2007
DE wrote:

> Hello,
>
> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();
> }
> popMatrix();
>
> The curly brackets have no functional meaning but increase the
> readability significantly. I want to be able to do the same thing in
> python. Since curly brackets are not available and indenting without
> an if or while conditional doesn't work, I have started to question if
> this is possible in python at all.
>
> Any ideas ?


You could use

if True:
# do stuff

but I have no sympathy for such self-inflicted noise.

With Python 2.5 you can do even better -- you can emulate what should have
been RAII in your C++ example in the first place:

from __future__ import with_statement

from contextlib import contextmanager

def push_matrix():
print "push"

def pop_matrix():
print "pop"

@contextmanager
def matrix():
m = push_matrix()
try:
yield m
finally:
pop_matrix()

with matrix():
print "do stuff"
with matrix():
print "do more stuff"

Peter
 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      03-30-2007
DE wrote:

> The curly brackets have no functional meaning but increase the
> readability significantly.


Personally, I don't think so. It quite explodes the code.

Yes, I also indent "BSD style" in my C++ programs.

Regards,


Björn

--
BOFH excuse #175:

OS swapped to disk

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-30-2007
On Fri, 30 Mar 2007 02:04:45 -0700, DE wrote:

> Hello,
>
> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();
> }
> popMatrix();
>
> The curly brackets have no functional meaning
> but increase the readability significantly.


I don't understand why you are indenting
the function calls. What does the
indentation and spacing signify?

Or, to put it another way:


I don't understand why you
{
are indenting
{
the function calls.
}
What does the
}
indentation signify?



> I want to be able to do the same thing in
> python. Since curly brackets are not available and indenting without
> an if or while conditional doesn't work, I have started to question if
> this is possible in python at all.


Thank goodness it isn't, in general.

But if you want people to point at you and laugh in the street, you can do
this:

pushMatrix()
if True:
drawStuff();

pushMatrix();
if True:
drawSomeOtherStuff()

popMatrix();

popMatrix();



> Any ideas ?


Some people
have a strange
idea of
"increase
readability".


--
Steven.

 
Reply With Quote
 
Mark Jackson
Guest
Posts: n/a
 
      03-30-2007
"DE" <> writes:
> Hello,
>
> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();
> }
> popMatrix();
>
> The curly brackets have no functional meaning but increase the
> readability significantly.


You are e. e. cummings, and I claim my £5.

--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
Every 10 years we say to ourselves, "If only we had
done the right thing 10 years ago."
- Thomas Friedman


 
Reply With Quote
 
DE
Guest
Posts: n/a
 
      03-30-2007
Thanks Peter. This sounds like to right solution for my case, because
in addition to indentation, I can automate push and pop. I'll
investigate this further. I appreciate.

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      03-30-2007
"DE" <> wrote:

> Here is what I do in C++ and can not right now in python :
>
> pushMatrix()
> {
> drawStuff();
>
> pushMatrix();
> {
> drawSomeOtherStuff()
> }
> popMatrix();
> }
> popMatrix();
>


If I understand this contortion is because you have some sort of stack
and you want the code to follow the depth as you push and pop things
from the stack.

If you write this in Python then when drawSomeOtherStuff() throws an
exception your 'stack' will get messed up, so you'll need to add some
more code to handle this. Using Python 2.5 this is the sort of thing you
should end up with (and you'll notice that your indentation appears
naturally when you do this):


from __future__ import with_statement
from contextlib import contextmanager

# Dummy functions so this executes
def pushMatrix(arg): print "pushMatrix", arg
def popMatrix(arg): print "popMatrix", arg
def drawStuff(): print "drawStuff"
def drawSomeOtherStuff(): print "drawSomeOtherStuff"

# The matrix stack implemented as a context handler.
@contextmanager
def NewMatrixContext(arg):
pushMatrix(arg)
try:
yield
finally:
popMatrix(arg)

# and finally the function to actually draw stuff in appropriate
# contexts.
def fn():
with NewMatrixContext(1):
drawStuff()
with NewMatrixContext(2):
drawSomeOtherStuff()

fn()
 
Reply With Quote
 
DE
Guest
Posts: n/a
 
      03-30-2007
>
> I don't understand why you are indenting
> the function calls. What does the
> indentation and spacing signify?


The indentation of function calls increases readability not in the
sense that it is easier to decrypt the code, but rather it is
analogous to the coordinate system transformations these matrix push
and pop calls perform..

 
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
remove overall indentation preserving reletive indentation Jesse B. Ruby 2 03-27-2010 07:23 PM
code style and readability [was: Re: Checking the boolean value of acollection] Marco Bizzarri Python 8 09-13-2008 08:14 PM
Problem of Readability of Python Licheng Fang Python 35 10-18-2007 10:24 AM
'academic' problem ( speed/memory efficiency vs. human readability and easy design ) burningsunorama@gmail.com C++ 9 07-28-2006 05:22 PM
Is this just for readability to the developer? python David Stockwell Python 3 06-02-2004 01:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57