473,771 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indentation

every time I switch editor all the script indentation get mixed up,
and python start giving me indentation weird errors.
indentation also hard to follow because it invisible unlike brackets
{ }

is there any solution to this problems?

thank you!
Oct 19 '08 #1
28 1512
On Sun, 19 Oct 2008 07:16:44 -0700, Gandalf wrote:
every time I switch editor all the script indentation get mixed up, and
python start giving me indentation weird errors. indentation also hard
to follow because it invisible unlike brackets { }
Indentation is not invisible.

Can you really not see that this text
is indented? If so, your news program
is seriously broken.

is there any solution to this problems?
Discipline. Choose a standard indent and stick to it.

You can use tabs, or spaces. If you use spaces, you can choose 4 spaces,
or 8, or any number, but whatever you choose, stick to it no matter what
editor you use. Good editors will let you use the tab key to indent with
spaces. Bad editors (e.g. Windows Notepad, and I feel your pain if you
have to use it) force you to manually insert spaces.

Especially never mix tabs and spaces in the same file. If you're editing
an existing file, you must follow whatever indent standard is already in
use.

You can also pass the -t option when launching the Python interpreter to
warn about mixed tabs and spaces.

See also the standard module tabnanny:

http://effbot.org/librarybook/tabnanny.htm

--
Steven
Oct 19 '08 #2
On 19 Oct 2008 14:34:45 GMT, Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrote:
On Sun, 19 Oct 2008 07:16:44 -0700, Gandalf wrote:
>every time I switch editor all the script indentation get mixed up, and
python start giving me indentation weird errors. indentation also hard
to follow because it invisible unlike brackets { }
....
>is there any solution to this problems?

Discipline. Choose a standard indent and stick to it.
Doesn't pretty much everyone use spaces and a four-position indent? I
don't think I've ever come across any half-decent Python code which
didn't follow that convention.

....
Especially never mix tabs and spaces in the same file.
Actually, the only really likely reason he sees "mixed up" indentation
is that he has mixed TAB/space source code *and* a misconfigured[0]
editor which sets the TAB stops at anything else than every 8th
character.

If I was him, I'd check and fix my editors first, and/or tell my
coworkers to stop emitting broken[0] TABs.

/Jorgen

[0] This is an old and tedious topic ... my view on TABs is that they
are useless iff they aren't rendered the same way everywhere. The
size 8 is hard-coded into terminals, printers and programs since
ancient times; thus anything else is wrong.

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Oct 19 '08 #3
On Sun, Oct 19, 2008 at 06:05:08PM +0000, Jorgen Grahn wrote:
Doesn't pretty much everyone use spaces and a four-position indent?
I can't speak for everyone, or even "pretty much everyone"... but I
know of several people who favor the idea of "indent with tab, align
with space." The advantage to this scheme is that anyone using a
half-sane editor can very easily change the level of indentation to
their preference, meanwhile keeping the rest of the code aligned
properly (though this may well interfere with keeping line lengths to
80 columns, or some other decided-upon number). I favor this myself
actually, though I rarely use it for Python code I write, because that
almost invariably needs to work with someone else's code who insists
on the "standard" you mentioned.

I know plenty of people who prefer a full 8-column indent, feeling that
it makes indentations (and therefore the logical blocks wich the
indentation is meant to indicate) much clearer, though most of them
are primarily C coders. Some switch to 4 for python, and some prefer
to keep 8 for pretty much everything they write.
I don't think I've ever come across any half-decent Python code
which didn't follow that convention.
I have. :) Unless one defines a lack of tabs as a criteria of
"half-decent Python code" -- which I obviously don't.
[0] This is an old and tedious topic ...
This is very true... though clearly to anyone who hasn't encountered
it before, it is rather new.
my view on TABs is that they are useless iff they aren't
rendered the same way everywhere. The size 8 is hard-coded into
terminals, printers and programs since ancient times; thus
anything else is wrong.
This, on the other hand, is quite false -- not your opinion, perhaps,
but all of the facts you've put forth in support of it. The tab size
of nearly every tty device I've interacted with in the last 25 years
*defaulted* to 8, but is configurable using any of various terminal
control programs, such as tabs, stty, etc. (though I wouldn't know how
to do this on Windows, or if it's even possible/relevant)... The
utility of adjustable tabs is what I already stated above. I'm not
saying you should change it... just that it is very much *not*
hard-coded. In fact, most of the terminal devices I've used let you
set arbitrary tab stops at whatever column positions you like.
Occasionally useful, though not to me personally.

One thing is for sure: it's essential that whatever formatting you
decide to use, everyone touching that code needs to use the same one,
or else the result is an annoying mess. Vim (and quite probably other
editors) solves this by providing a way to set the options in the file
you're editing, which is one of many reasons why I favor it over
anything else. For example, at the top of your file:

#!/usr/bin/python
# vim:ts=4:sw=4:e xpandtab

Though of course, using this kind of mechanism quickly becomes gross
if everyone is using a different editor, and they all support a
similar but different mechanism for doing so.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFI+4+jdjd lQoHP510RApbJAJ 9kUc7fZ7WLw19Yo ruvOsOzEylmjgCd HW9x
rMQ8qfjQ+DyQew5 o6bN5gSw=
=qpRT
-----END PGP SIGNATURE-----

Oct 19 '08 #4
Derek Martin:
I know of several people who favor the idea of "indent with tab, align
with space." [...] I favor this myself actually, [...]
Thanks Guido, in Python3 this is finally a Syntax Error (I have asked
for this probably about three years ago).

Unfortunately the new Python-syntax-based Delight language starts with
a half-bad foot regarding indents:
http://delight.sourceforge.net/syntax.html

Bye,
bearophile
Oct 19 '08 #5
Gandalf <go******@gmail .comwrites:
every time I switch editor all the script indentation get mixed up,
and python start giving me indentation weird errors.
indentation also hard to follow because it invisible unlike brackets
{ }

is there any solution to this problems?
Follow PEP 8 <URL:http://www.python.org/dev/peps/pep-0008for all
code you write.

Follow whatever convention exists in all code you edit.

Agitate for consensus if you edit code which is inconsistent.

--
\ “Geeks like to think that they can ignore politics. You can |
`\ leave politics alone, but politics won't leave you alone.” |
_o__) —Richard Stallman, 2002-07-26 |
Ben Finney
Oct 19 '08 #6
On Sun, 19 Oct 2008 15:50:59 -0400, Derek Martin <co**@pizzashac k.orgwrote:
On Sun, Oct 19, 2008 at 06:05:08PM +0000, Jorgen Grahn wrote:
>Doesn't pretty much everyone use spaces and a four-position indent?

I can't speak for everyone, or even "pretty much everyone"... but I
know of several people who favor the idea of "indent with tab, align
with space." The advantage to this scheme is that anyone using a
half-sane editor can very easily change the level of indentation to
their preference, meanwhile keeping the rest of the code aligned
properly (though this may well interfere with keeping line lengths to
80 columns, or some other decided-upon number).
I'm happy to say I have never seen the kind of code you mention. If I
understand you correctly, if you sent me code to look at, I would
either have to change the TAB setting of all relevant tools to
four-spaced TABs (remembering to restore them afterwards), or suffer
looking at the code eight-indented -- twice the one I'm used to.
I favor this myself
actually, though I rarely use it for Python code I write, because that
almost invariably needs to work with someone else's code who insists
on the "standard" you mentioned.
Yes; what you do makes sense in a way, but it seems very incompatible
with what most people do.

What has made me dread alternate TAB settings is not the style you
describe, but the uncontrolled one where some lines are indented with
oddly-sized TABs and others (partly) with spaces. When trying to read
such code, it's not uncommon that you have to try several different
TAB sizes before you hit the one where the code stops looking like
gibberish. And often you don't find one, because someone beat you to
it and the code now uses several different TAB settings in different
parts of the file ... At least if it's Python code, chances are good
that it refuses to load such code, or that it crashes soon.

....
>[0] This is an old and tedious topic ...

This is very true... though clearly to anyone who hasn't encountered
it before, it is rather new.
> my view on TABs is that they are useless iff they aren't
rendered the same way everywhere. The size 8 is hard-coded into
terminals, printers and programs since ancient times; thus
anything else is wrong.

This, on the other hand, is quite false -- not your opinion, perhaps,
but all of the facts you've put forth in support of it. The tab size
of nearly every tty device I've interacted with in the last 25 years
*defaulted* to 8, but is configurable using any of various terminal
control programs, such as tabs, stty, etc. (though I wouldn't know how
to do this on Windows, or if it's even possible/relevant)...
Ok, I stand corrected. I shouldn't have brought it up, because the
defaults are what matters. Since text files contain no metainformation
about what TAB size they use, you have no chance to reconfigure your
tools for every text file they process.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Oct 19 '08 #7
On Sun, 19 Oct 2008 19:03:29 +0200, Bruno Desthuilliers wrote:
Steven D'Aprano a écrit :

(snip)
>You can use tabs, or spaces. If you use spaces, you can choose 4
spaces, or 8, or any number,

By all means, make it 4 spaces - that's the standard.
It's *a* standard. I believe it is the standard for the Python standard
library, but there are other standards.
--
Steven
Oct 19 '08 #8
Steven D'Aprano a écrit :
On Sun, 19 Oct 2008 19:03:29 +0200, Bruno Desthuilliers wrote:
>Steven D'Aprano a écrit :

(snip)
>>You can use tabs, or spaces. If you use spaces, you can choose 4
spaces, or 8, or any number,
By all means, make it 4 spaces - that's the standard.

It's *a* standard. I believe it is the standard for the Python standard
library, but there are other standards.
I can't remember having seen any other "standard" so far.
Oct 20 '08 #9
Bruno Desthuilliers <br************ ********@websit eburo.invalidwr ote:
>I can't remember having seen any other "standard" so far.
I've seen various indentation styles used in examples on this newsgroup.
No one bothers to complain when I indent using tabs, or someone else
uses 2 space indentation.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.u waterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Oct 20 '08 #10

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

Similar topics

0
1255
by: Magnus Lie Hetland | last post by:
Not many members on the Atox mailing list yet, so I'll venture a request here... In Atox 0.2, I've added support for indentation tokens (somewhat like the Python indentation scheme, but a bit more relaxed). If anyone finds that sort of thing interesting, please have a look and give me your opinion -- does it seem like a useful way of dealing with this sort of thing? (It seems that lookahead is more sorely needed with indentation, to
147
7787
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
177
7079
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
5875
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
9
3179
by: John Salerno | last post by:
How do you make a single string span multiple lines, but also allow yourself to indent the second (third, etc.) lines so that it lines up where you want it, without causing the newlines and tabs or spaces to be added to the string as well? Example (pretend this is all on one line): self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\n'
135
7524
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.
4
1801
by: bearophileHUGS | last post by:
This is the best praise of semantic indentation I have read so far, by Chris Okasaki: http://okasaki.blogspot.com/2008/02/in-praise-of-mandatory-indentation-for.html A quotation: I have appreciated that article, and I have personally seen how fast students learn Python basics compared to other languages, but I think that it's way more than just indentation that makes the Python language so quick to learn .
43
1982
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) {
1
1685
by: Eric S. Johansson | last post by:
in trying to make programming in Python more accessible to disabled programmers (specifically mobility impaired speech recognition users), and hitting a bit of a wall. The wall (for today) is indentation. I need a method of getting the "right indentation" without having to speak a bunch of unnecessary commands. For example, depth specified by the previous line. But, frequently you need to go to a more arbitrary indentation for example...
19
2105
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
9619
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
9454
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
10260
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...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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...
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.