Understanding the Use of Indentation and Capitalization in IronPython

Must Read

Local FM Radio (Radio Tuner)

Local FM Radio provides a unique interface to your phone’s built-in FM radio tuner. Unlike the built-in radio app...

Golf Gifts and Decor Make Perfect Holiday Gifts

November 23, the day after Thanksgiving, also know as "black Friday", starts the holiday shopping countdown for 2007. Only...

Home Mortgage Refinance Explained

Refinancing is often considered one of the most beneficial ways to save money on your home mortgage.  Refinancing is...

Most application programming languages have rules that help the compiler or interpreter understand what you mean. For example, when working with C, C++, Java, C#, and a number of other languages, you use opening and closing braces to indicate the code that belongs within a structure such as a function or loop. Without these opening and closing braces, the compiler or interpreter for the target language would never be able to understand what you mean — these braces add structure to your application code. Likewise, IronPython relies on rules, indentation and capitalization, to help the interpreter understand your code.

The interpreter does help you with the indentation. The amount you indent a line doesn’t seem to matter. Using tabs instead of spaces doesn’t seem to matter either, but using tabs will ensure that you don’t run into problems seeing the indentation in code properly. Open a copy of IPY to follow along with the discussion in this section. Try the following steps and you’ll discover how the interpreter helps you discover when to indent.

  1. Type SomeText = ‘Hello‘ and press Enter. You’ll see that the interpreter adds three greater-than signs (>>>). The >>> is a primary prompt and tells you that you don’t need to add any indentation.
  2. Type if SomeText == ‘Hello‘: and press Enter. Now you’ll see that the interpreter adds three dots (…) to the next line. The … is the secondary prompt. It tells you that you’ve entered a structure.
  3. Type print SomeText and press Enter. The interpreter displays an error message like the one shown in Figure 2-12. The interpreter expected an indented block, but you didn’t provide any indentation, so the block failed.
  4. Repeat Step 2 again. Press Tab. Type print SomeText and press Enter. This time, the entry succeeds, as shown in Figure 2-13. Notice that the interpreter has displayed … again so that you can continue the block if desired. In fact, the block continues until you end it by not indenting an entry.
  5. Press Enter. The interpreter displays the word Hello. The interpreter will keep track of a block until you finish entering the last line of code. It then evaluates the block and performs all of the tasks it contains.

Not indenting at a secondary prompt produces an error.

A properly indented entry succeeds.

Now that you have the basic idea about indentation, it’s time to consider capitalization. IronPython is case sensitive. So, if you type:

If SomeText == ‘Hello’:

and press Enter, the code will fail. Figure 2-14 shows that the error information you receive isn’t straightforward. All that the interpreter tells you is that the token it received is unexpected (a token is a single word that the interpreter parses to discover what you want to do). The interpreter will provide more precise information for some errors. Type:

if SomeText == ‘Hello’:
print sometext

and press Enter. This time you get precise error information, as shown in Figure 2-15. The interpreter tells you that the error lies in line 2 of the block and that the error occurred because SomeText isn’t defined.

Some error messages are ambiguous.

The interpreter can provide precise error information for certain classes of error.

The point of this exercise is that the interpreter will catch indentation and capitalization errors. In some cases, you might scratch your head for a while trying to figure out what went wrong, but the interpreter is accurate about locating such problems for you.

It doesn’t matter whether you use tabs or spaces for indentation. Try the example out using spaces instead of a tab. You’ll discover that it works just fine. However, using tabs provides uniform spacing that is easier to see when you’re reading the code. If you’re not careful, you might use one space in one location and four in the next, making it hard to tell when something really is indented.


Please enter your comment!
Please enter your name here

Latest News

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits,...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

High Key Lighting Techniques for Professional Photographers

I’ve written many times about high key lighting techniques and how to achieve them. The term “high key” is a bit misleading. As I’ve...

More Articles Like This