[OT] Re: Variable scoping rules in Python?
- From: Peter Otten <__peter__@xxxxxx>
- Date: Mon, 8 Oct 2007 19:03:13 +0200
joshua.davies wrote:
Ok, I'm relatively new to Python (coming from C, C++ and Java). I'm
working on a program that outputs text that may be arbitrarily long,
but should still line up, so I want to split the output on a specific
column boundary. Since I might want to change the length of a column,
I tried defining the column as a constant (what I would have made a
"#define" in C, or a "static final" in Java). I defined this at the
top level (not within a def), and I reference it inside a function.
Like this:
COLUMNS = 80
def doSomethindAndOutputIt( ):
...
for i in range( 0, ( len( output[0] ) / COLUMNS ) ):
print output[0][ i * COLUMNS : i * COLUMNS + ( COLUMNS - 1 ) ]
print output[1][ i * COLUMNS : i * COLUMNS + ( COLUMNS - 1 ) ]
..
etc. etc. It works fine, and splits the output on the 80-column
boundary just like I want.
Just in case it's not intentional: You'll lose every 80th character as
python intervals do not include the upper bound. The same problem
affects the for loop -- e. g. when output[0] has less than COLUMNS
columns nothing is printed:
[]range(0, 79/80)
Peter
.
- References:
- Variable scoping rules in Python?
- From: joshua . davies
- Variable scoping rules in Python?
- Prev by Date: Re: Really basic problem
- Next by Date: Re: Variable scoping rules in Python?
- Previous by thread: Re: Variable scoping rules in Python?
- Next by thread: Re: Variable scoping rules in Python?
- Index(es):