Using .NET Data Types

When you work with .NET in IronPython, you have full access to every type that .NET supports. However, you don’t always create these types as you would in another language. For example, when working with C# or Visual Basic, you simply declare a variable of a certain type and then make an assignment to it. When working in IronPython, you must remember that making an assignment creates a Python type, not a .NET type. For example, let’s suppose you create a UInt32 variable and then make an assignment to it. Figure 7-10 shows the sequence of events that will occur.

As you can see, when you initially create the variable, IronPython recognizes it as a UInt32. In fact, even the type() function knows that this is a UInt32. When the code makes a simple assignment, however, notice that IronPython changes the type to a simple int. A check using type() shows that the data type has indeed changed. In order to change the value of a UInt32, you must make another assignment using the UInt32() constructor.

Figure 7-10: .NET and Python variables don’t mix very well in most cases.
Figure 7-10: .NET and Python variables don’t mix very well in most cases.

You can easily convert Python variables to their .NET counterparts in many cases. For example, you can use the following code to create a Python variable and then use it to create a .NET variable.

[code]

PVar = 5
NetVar = System.UInt32(PVar)

[/code]

In some cases, you’ll find that a direct conversion won’t work for any of a number of reasons. For example, you might find that converting Python numeric variables won’t work in some cases because .NET is expecting a string. In this case, you can try the clr.Convert() method. Here’s an example of using clr.Convert() to produce the same results as the previous example. (Remember that you must type import clr before you can use it.)

[code]

NetVar2 = clr.Convert(PVar, System.UInt32)

[/code]

The first argument contains the Python variable. The second argument contains the .NET data type you want as output. Of course, you must ensure you have imported the .NET assembly that contains the desired output type because the call will fail otherwise. In fact, any time clr.Convert() fails to convert the data, you get a TypeError.

Of course, before you can convert anything, you need to know the best type to use for the conversion. For example, you run the type() function against a Python variable to obtain the type information, but don’t know the equivalent .NET type. In this case, you can use clr.GetClrType() to obtain the closest .NET equivalent, as shown here.

[code]

# Produces System.Int32 as output.
clr.GetClrType(int)
# Produces System.String as output.
clr.GetClrType(str)

[/code]

The clr.GetPythonType() method obtains the Python equivalent of .NET data types. For example, if you type clr.GetPythonType(System.Int32), you get int as output. Of course, you’ll want to know how to convert your .NET data into a Python equivalent. In this case, you simply rely on the Python functions you use to create variables of specific types. For example, the following code converts a .NET UInt32 into a Python long.

[code]

MyVar = long(NetVar)

[/code]

Some .NET to Python conversions will result in data loss and Python won’t warn you about the problem. However, Python normally chooses a data type override that will work. For example, if you try to convert a number that won’t fit into an int, Python will automatically choose a long for you. Make sure that you always test a conversion before you assume it will work.

 


Posted

in

by

Tags: