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 WorldThe 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 ObjectsThe 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:
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 OverloadsIf 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 :
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:
Other Examples of Compiler TechniquesThe 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. |