Completely INNOCENT Indentation question

V

valued customer

Is there a way in python to indicate that you want your
"base level of indentation" to be four spaces instead of
zero?

As far as I know now, the only way is by introducing some
sort of language statement, for example:

if (1):
### begin python script
foo = 1
bar = 2
print foo
print bar

Is there a way to accomplish this without introducing
the "throwaway if statement" ??
 
C

Cliff Wells

Is there a way in python to indicate that you want your
"base level of indentation" to be four spaces instead of
zero?

As far as I know now, the only way is by introducing some
sort of language statement, for example:

if (1):
### begin python script
foo = 1
bar = 2
print foo
print bar

Is there a way to accomplish this without introducing
the "throwaway if statement" ??

I don't think so. Just out of curiosity, why would you want to do that?
If you're unable to see the leftmost characters, just try dragging your
editor window a bit to the right <wink>
 
D

Darren Kirby

quoth the valued customer:
Is there a way in python to indicate that you want your
"base level of indentation" to be four spaces instead of
zero?

As far as I know now, the only way is by introducing some
sort of language statement, for example:

if (1):
### begin python script
foo = 1
bar = 2
print foo
print bar

Is there a way to accomplish this without introducing
the "throwaway if statement" ??

def main():
#python script here

if __name__ == '__main__':
main()

Will that work for ya?

--
Darren Kirby :: Part of the problem since 1976 :: http://badcomputer.no-ip.com
Get my public key from
http://keyserver.linux.it/pks/lookup?op=index&search=bulliver
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBBbTL+OrzWcOwL7mwRAt1KAJ9vAzsxqTTmqkhmZXgzcriG/UGlBwCgnXuL
8+9V76yuK8KpnzjsgN/o/Q4=
=aAra
-----END PGP SIGNATURE-----
 
V

valued customer

Is there a way in python to indicate that you want your
I don't think so. Just out of curiosity, why would you want to do that?

The question was just to make sure I hadn't overlooked some
feature or setting of the programming language that would enable
what I had asked about (short of introducing any of the various
language constructs that we all can imagine and you need not
elaborate here).

As far as 'why would anyone want to do that' one reason that
never fails is *personal preference* ... there probably isn't
a stylistic or programming practice in existence that can't be
explained at least partially by that. Therefore if someone asks,
there must be at least *that* reason.

For those who are not satisfied with that kind of answer, here
is an example:

Suppose you are editing source code in such a way
that you only want *comments* to be flush left, and the rest of
the code to be indented.

(but Why would you want do that?)

Because you work with a text editor that shows an 'outline'
of your code based on the indentation, and you want the outline
structure to be reflected in the comments.

### documentation
""" This is a python script that does some stuff.
Not only does it do stuff, it's documented stuff.
"""
### initialize python
import foo
import bar

### do some stuff
print foo.frobilize()

### do some other stuff
print bar.babelize()

### end of python


Yes the question may remain ... why would anyone wanna do
that? Well, there are reasons. Whatever the justifications
may be, the 'additional language statements' workaround will
apparently have to suffice.

Thanks for the input though.
 
J

Josiah Carlson

Because you work with a text editor that shows an 'outline'
of your code based on the indentation, and you want the outline
structure to be reflected in the comments.

### documentation
""" This is a python script that does some stuff.
Not only does it do stuff, it's documented stuff.
"""
### initialize python
import foo
import bar

### do some stuff
print foo.frobilize()

### do some other stuff
print bar.babelize()

### end of python


Yes the question may remain ... why would anyone wanna do
that? Well, there are reasons. Whatever the justifications
may be, the 'additional language statements' workaround will
apparently have to suffice.


Considering that many text editors, program editors, IDEs, etc., come
with syntax highlighting, would using different colors for comments be
sufficient? They tend work well for me. In terms of an outline, many
editors also have the ability to pull out comments of a certain format,
pull out function and class names, etc.

I'm curious as to what editor you use that has such an outline feature.

- Josiah
 
T

Tim Roberts

As far as 'why would anyone want to do that' one reason that
never fails is *personal preference* ... there probably isn't
a stylistic or programming practice in existence that can't be
explained at least partially by that. Therefore if someone asks,
there must be at least *that* reason.

Unfortunately, it's impossible to satisfy "personal preferences" for
spacing in a language where spacing is a fundamental part of the syntax.
It's somewhat similar to asking why # could not be used as a comment
character in C.
 
V

valued customer

Unfortunately, it's impossible to satisfy "personal preferences" for
spacing in a language where spacing is a fundamental part of the syntax.
It's somewhat similar to asking why # could not be used as a comment
character in C.

Makes sense, although the original post does not ask *why*
such-and-such is
(or is not) possible, but *whether* it is possible. Moreover, just
because
such-and-such is a *fundamental* part of the syntax, it does not
necessarily
follow that it is an *immutable* part of the syntax.

As you know, some
languages and syntax formats actually *do* allow things like alternate
comment delimters, alternate quotation delimters, and even alternate
methods of specifying INDENT and DEDENT tokens.

Apparently, at least for the latter case, python does not ... which is
all the original question is asking.

(By the way ... For those curious about "outlining" text editors,
there are several out there ... just do a web search on "code folding"
to see some examples...)
 
V

valued customer

2. Write a preprocessor (in python) that you supply with your program as
a command-line argument
As a routine user of similar 'preprocessor tricks' to coerce other,
more klunky languages into behaving nicely, that's a great
suggestion, seems like an obvious solution (in hindsight).
Python makes me very lazy. Writing a report in LaTeX today, it was
actually easier for me to make up a syntax for tagging math symbols
With all the emphasis on "extensibility" these days, it seems the best
mechanism for extensibility is the unsung hero ... code comments ...
You can do things the original designers never anticipated.
 
C

Caleb Hattingh

Actually, it should be trivial.

1. Set the preferance in your editor
2. Write a preprocessor (in python) that you supply with your program as
a command-line argument

Your preprocessor de-dents the code. Tracebacks will still be meaningful,
because you are not changing line numbering. BTW, The code-folding in ViM
can be set to correspond to indent-level, which means you can fold and
unfold all constructs to a specific depth. Magic.

Python makes me very lazy. Writing a report in LaTeX today, it was
actually easier for me to make up a syntax for tagging math symbols (put
into comments in the LaTeX source), and extract those and build a List Of
Symbols with a python preprocessor before texification, rather than find
(and figure out how to use) the correct LaTeX package for that. More
magic.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top