Considering Data Types in IronPython

0
151

You might have noticed an appalling lack of data types in the example so far. When an application needs a string variable, it simply assigns the string to a variable without ever specifying a data type. Likewise, numbers are simply assigned to a variable; it doesn’t apparently matter  whether the number is an integer or a floating point value. IronPython seemingly doesn’t care. For example, the code in Listing 2-1 works just fine.

Listin g 2-1: Working with variables of various types

String = “Hello”
Integer = 1
Float = 1.5
print “String =”, String, “nInteger =”, Integer, “nFloat =”, Float
raw_input(‘Press any key to continue…’)

This example shows that you can create a string, integer, or floating point value without actually declaring them. IronPython does track the data type, but it does so in the background, without your knowledge. When you try to print the values, IronPython automatically converts the values in the background for you to standard strings. Figure 2-16 shows the output of this example.

You may also notice that this example makes use of escape characters, the n in this case. The n simply tells IronPython to add a newline character to the output. In fact, if you’ve worked with C, C++, C#, or any of a number of languages that rely on escape codes, you’ll find yourself right at home when working with IronPython. Table 2-1 shows the common escape codes that IronPython recognizes.

IronPython tracks data type and performs conversions automatically as needed.

IronPython Common Escape Codes

The raw_input() function is also new. If you’re working in Visual Studio, the console screen appears and disappears so quickly in many cases that you can’t see the output of your code. Using the raw_input() function as shown simply adds a pause to your code. The string included with the function call provides a prompt. You’ll discover more about input and output functionality as the book progresses. For right now, all you really need to know is that you can obtain a value from the console using raw_input().

Don’t get the idea that you won’t have any access to native data types. It’s possible to tell IronPython to react in a certain way to your data. For example, you can use the str() and repr() functions to interpret a string in multiple ways. In addition, you can specifically convert  numbers to a specific type, as shown in Listing 2-2.

Listin g 2-2: Specifying data types

# Create a string variable.
String = ‘Hello and Goodbye’
# Display using str() and repr()
print ‘A string and its representation.’
print ‘str()‘, str(String)
print ‘repr()‘, repr(String)
# Create the numeric variable.
Number = 5.1
# Display the numeric types.
print ‘nThe numeric data types.’
print ‘int’, int(Number)
print ‘long’, long(Number)
print ‘float’, float(Number)
print ‘complex’, complex(Number, 3.5)
# Pause the display
raw_input(‘Press any key…’)

This example begins by showing you a comment. Yes, IronPython, like most computer languages, supports comments and you should use them often.

The first part of the code works with a string. When you use the str() function, you tell IronPython to take any data within the function, the expression, and convert it to a string. IronPython performs this task for you automatically, but you can use the str() function for explicit conversion. The output of the str() function is always in human-readable form. The repr() function returns a representation of the object you pass to it. In most cases, you use the repr() function to create output that the interpreter understands. Figure 2-17 shows the difference in output between the two functions. Notice that repr() outputs single quotes so you could pass this information directly to the interpreter.

The second part of the code works with a number. In this case, the number contains floating point data. However, you can convert it directly to an int or long using the int() or long() functions. The float() function will convert any numeric data into a floating point number and you can even create complex numbers using the complex() function. Figure 2-17 shows the output from the numeric conversions as well. IronPython also supports base conversions using the oct() and hex() functions. In fact, you can see the entire list of built-in functions at http://docs.python.org/library/functions.html.

Use str() or repr() as appropriate to create output for your application.

You’ll see a wealth of data types as the book progresses. For example, Chapter 4 discusses arrays and collections, Chapter 5 tells you all about objects, and Chapter 6 begins a discussion of working with the standard library. There are a number of online resources for working with data types as well. The two best sources — the ones used for this book — are at:

  • http://www.python.org/doc/2.4.4/lib/types.html
  • http://docs.python.org/library/datatypes.html