Array

Using .NET Data Types

Must Read

Nikon D7000 Formatting Your Memory Card

Now that you have your card, let’s talk about formatting for a minute. When you purchase any new SD...

The Most Happening and Joyous Day i.e Black Friday Is in Nov

Black Friday is the Friday following Thanksgiving Day in the United States, it is the the beginning of the...

Home Equity Loans Tips Guide 101

A Home Equity Loan is considered as the best friend for those with bad credit, however, this best friend...
Admin
test

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.

 

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