Using Python for .NET


Unlike CPython, Python for .NET provides no interactive session. Thus, all Python programs must be saved as .py files and compiled using the Python for .NET compiler. By default, the compiler will generate an executable that can be run, but compiler options allow a variety of options. Invoking the compiler with a -help option will show all supported options.

The compiler itself can be downloaded from the official Web site (http://www.python.org). CPython, the Python for Win32 extensions, and the .NET SDK (for the C# compiler) are all required; see the readme file for more information.

Example: Hello World

The simplest Python program is

 print "Hello, world" 

Assuming this code is saved as hello.py , we can compile the program simply by executing the following command:

 C:\source> python C:\Python.Net\compiler\cpy.py hello.py 

This statement will compile the source code and create hello.exe . We can execute this program to see the result:

 C:\source> hello.exe  Hello, world C:\source> 

Using .NET Objects

The Python for .NET compiler recognizes a special COR [4] namespace, which identifies .NET objects. Thus, any object whose name is prefixed with COR is assumed to be a reference to an external .NET object. For example, the .NET Base Class Library defines a System.Collections namespace, which contains a Queue class. A Python program could create an instance of this Queue class with the following code:

[4] This name is a throwback to the early days, before .NET was named .NET.

 q = COR.System.Collections.Queue() 

Any namespace can be specified. It is your responsibility to add any dependent modules (other than from the .NET Base Class Library) using the /ref compiler option.

Once instantiated , the Queue object can be used naturally. For example,

 q.Enqueue("Hello there")  print q.Count 

calls the Queue 's Enqueue method and fetches the Count property.

Method Signatures and Overloads

If a public signature is generated, the compiler still needs to determine the .NET types to use in the function signature. To this end, the compiler uses a number of hints before defaulting to a basic, default signature.

The first thing the compiler does is look for some type hints in the Python source code. As the basic CPython parser is used, no syntactic changes to the language could be made, so the compiler looks for variables of a special name inside the function to provide type information. It recognizes two variables :

  • _com_params_ is a string with a comma-separated list of .NET type names .

  • _com_return_type_ is a string that holds the name of the .NET return type.

In both cases, the type name should be a fully qualified name and may be any .NET type in any referenced assembly.

For example, the Python code

 def func(self, name, num):      _com_params_="System.String, System.Int32"     _com_return_type_="System.Int32"     ... 

will generate a function with the .NET signature similar to what would be generated by the C# compiler for

 Int32 func(string name, Int32 num) 

This feature is useful in a number of situations:

  • When the base class from which you are implementing contains overloaded virtual methods you wish to implement. These methods all have the same name, but differ in terms of the number and types of parameters. This allows you to specify exactly which version of the method you are overloading.

  • When you are implementing a class you expect other language to inherit from and wish to specify the exact signatures that should be used by subclasses.

Other Examples of Compiler Techniques

The compiler test suite shows a number of other techniques that can be used by the compiler. Examples include type conversion, arithmetic conversion, and use of the standard Python types. The test suite can be found in the compiler\suite directory of the distribution.



Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net