473,804 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indentation for code readability

DE
Hello,

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

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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

Mar 30 '07 #1
16 2532
DE schrieb:
Hello,

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

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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 8) 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
Mar 30 '07 #2
On Mar 30, 7:04 pm, "DE" <devrim.er...@g mail.comwrote:
Hello,

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

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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()
(
drawSomeOtherSt uff()
)
[
drawEvenMoreStu ff()
]
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

Mar 30 '07 #3
DE wrote:
Hello,

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

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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
Mar 30 '07 #4
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,
Bjrn

--
BOFH excuse #175:

OS swapped to disk

Mar 30 '07 #5
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();
{
drawSomeOtherSt uff()
}
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:
drawSomeOtherSt uff()

popMatrix();

popMatrix();
Any ideas ?
Some people
have a strange
idea of
"increase
readability".
--
Steven.

Mar 30 '07 #6
"DE" <de**********@g mail.comwrites:
Hello,

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

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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
Mar 30 '07 #7
DE
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.

Mar 30 '07 #8
"DE" <de**********@g mail.comwrote:
Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherSt uff()
}
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 drawSomeOtherSt uff() 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 drawSomeOtherSt uff(): print "drawSomeOtherS tuff"

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

# and finally the function to actually draw stuff in appropriate
# contexts.
def fn():
with NewMatrixContex t(1):
drawStuff()
with NewMatrixContex t(2):
drawSomeOtherSt uff()

fn()
Mar 30 '07 #9
DE
>
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..

Mar 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2149
by: Hilbert | last post by:
Hello, I'm using python to output RIB streams for Renderman. The RIB stream is a bunch of statements which describes a 3d image. The Rib standard allows for blocks which we usually indent for better visualization for example: WorldBegin Color Surface "constant"
147
7794
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give better freedom to the user? Is there any plausible reason behind this? Cheers! Sateesh
3
2936
by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python + PythonNet: import CLR
177
7107
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
7
5879
by: diffuser78 | last post by:
I am a newbie to Python. I am mainly using Eric as the IDE for coding. Also, using VIM and gedit sometimes. I had this wierd problem of indentation. My code was 100% right but it wont run because indentation was not right. I checked time and again but still no success. I rewrote the code over again in VI and it ran. Can you please explain whats the trick behind the correct indentation. Thanks
135
7542
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
9
1584
by: Paddy | last post by:
I was just perusing a Wikipedia entry on the "off side rule" at http://en.wikipedia.org/wiki/Off-side_rule . It says that the colon in Python is purely for readability, and cites our FAQ entry http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
43
1992
by: Bill Cunningham | last post by:
I have had several complaints by some people who wish to help me and I wish to get the problem straight. I wrote this small utility myself and added some indentation and I wonder if it is acceptable. It does make source easier to read. #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc!=3) {
19
2109
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next year.... another initiative is exporting the speech recognition environment to the Linux context. In a nutshell, he dictated to application on Windows, it tunnels over the network to a Linux machine, and will allow you to cut and paste to and...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.