Array

Stringing Statements Together

Must Read

Paying for College: 4 Tips for Parents

Going to college during a recession is getting more difficult for U.S. students. The national unemployment rate is hovering...

2010 Walmart Cyber Monday TV Sales Ad Released in Early November – Vizio, Samsung and Philips HDTVs

Over the last several years it has been the case that more and more Americas are looking to take...

Obama’s Home Mortgage Refinance Or Modification Plan For 2009

President Obama is ready to help millions of homeowners save hundreds of dollars every month, or even their home...
Admin
test

A basic IronPython application begins as a series of statements, as a procedure. These statements perform some task at the command line or within a window, depending on which IronPython interpreter you use. This initial execution path is called the main function, even though there really isn’t a function — simply a set of statements.

A lot of people feel uncomfortable with the open feel of IronPython’s main function so they create something that looks a bit more like what they’re used to using, as shown in Listing 3-1.

Listin g 3-1: Creating an actual main() function

import sys
# Create a main() function that everyone can recognize.
def main(argv = None):
# Obtain the command line arguments.
if argv is None:
argv = sys.argv
# Display the command line arguments.
for ThisArg in argv:
print ThisArg
# Pause after the debug session.
raw_input(‘Press any key to continue…’)
# Call the main() function.
if __name__ == “__main__“:
main()

This code shows a few interesting structural features of IronPython, which is why it’s such a good example to start with. The code actually begins with a comparison of __name__ to “__main__“. It turns out that __name__ always contains the name of the current function. Using __name__ makes it possible for your application to detect its position in the execution loop.

Most programming languages provide an equivalent of the main() function described in Listing 3-1. Even C# and Visual Basic have a main()-type function that creates the initial form. Whether you actually use a main() function in your applications depends upon how you expect other developers to interact with your code. The examples in this book don’t rely on a main function for the sake of clarity, but you might want to consider including one when developers are used to working with main() as part of their coding experience. Of course, you can call main() anything you like to match the programming language that your developers normally use.

In this case, the application calls main(), the new main function. If you’ve spent any time writing C or C++ code, this version of main() will look a little familiar. While it lacks argc (the argument count), this main() does have an argv. However, the arguments can change between the time the code starts the application and begins processing main(), so you want to set argv to None (essentially, nothing at all in IronPython), and obtain the command line arguments from sys.argv.

At this point, the code uses a for loop to parse argv and displays its arguments on screen. At a minimum, you get the name of the application. The arguments you see depend on their order on the command line (or in Visual Studio). For example, if you specify -D after the filename, then you see it as part of the arguments. Figure 3-1 shows an example of what a series of arguments might look like when using a command line of NewMain.py -D -c These are arguments. The code ends by pausing so you can see the output when using the Visual Studio debugger.

Create a main() function and then process the command line arguments in it.Notice the levels of indentation in this example. The levels of indentation show the structure of the application. Unlike many application development languages, IronPython enforces indentation, with the result that you can see the application structure quite easily.

In IronPython, structure comes in a number of forms, all of which are accessible to you as the developer. Using structure properly makes your applications easier to understand. From the main (and any other) function, you can use the following structural elements:

  • Import external files
  • Call other functions
  • Use decision-making or loop structures
  • Interact with objects

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

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, even though it will have...

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...

More Articles Like This