Migrating from CPython3 (PythonNet2.5) to PythonNet3
Migrating from CPython3 (PythonNet2.5) to PythonNet3
This migration is simpler but involves fundamental changes in the management of .NET types.
Of course, everything mentioned in the previous section must be taken into consideration.
PythonNet3 aims to bring the “Python in Dynamo” experience closer to that offered by IronPython, while leveraging the CPython ecosystem (NumPy, Pandas, etc.). It is the industry standard for Python/.NET interoperability.
Major Benefits and Improvements
Features
PythonNet3(Python 3.7+/Python.NET v3)
Comments
CPython Ecosystem
Excellent access to the entire modern PyPI ecosystem (NumPy, Pandas, etc.).
.NET Collections Mechanism
Automatic conversion is removed. The object is a “view” of the .NET collection, without copying data, which improves performance. However, they implement the standard Python collections interfaces of collections.abc.
IEnumerable
PyObject now implements IEnumerable in addition to IEnumerable. A bug where all .NET class instances were treated as Iterable has been fixed.
(ex: hasattr(pyObject, "_iter_") )
LINQ
Ability to use LINQ method extensions on IEnumerable.
Dynamo feature
Method overloading
Support has been improved, including for methods with generic type parameters ().
C# Operators
Python’s binary and unary arithmetic operators now call the corresponding C# operator methods.
.NET Class Interfaces
Simplified support for .NET Class Interfaces.
Dynamo feature
Out or ref parameters
You can now overload .NET methods in Python that use ref and out parameters. To do this, you need to return the values of these modified parameters as a tuple.
C# Arithmetic Operators
Python’s binary and unary arithmetic operators now call the corresponding C# operator methods.
Heritage and Builders
If you overload the method init of a .NET type in Python, you must now explicitly call the base class constructor using super()._init_(...).
Conversions of enumerations
Implicit conversion between C# enums and Python integers is disabled. You must now use enum members (e.g.,MonEnum.Option ). Additionally, the .NET method Enum.Value.ToString() now returns the value name instead of its integer.
similar to IronPython
Details on some points
Implementing .NET Class Interfaces
In CPython3 (PythonNet2.5), it is impossible to easily use .NET class interfaces. This is now fixed with Dynamo_PythonNet3.
Notes:
A Python class that derives from a .NET class must have the attribute namespace
The class Custom_FamilyOption includes an example with ref and out parameters returning as a tuple
No conversion of .NET Collections and Arrays
This is the major change from CPython3 (PythonNet 2.5), which performed automatic implicit conversion.
With PythonNet3:
Automatic conversion is removed.
.NET collections and arrays now implement the standard Python collections interfaces (collections.abc).
The .NET object behaves like a “view”, which is efficient because there is no data copying.
You can use .NET methods like LINQ directly on these objects.
To use methods specific to Python lists (like append()) or bracket indexing, an explicit conversion via list() is required.
Consequences on the indexing of IEnumerable<T>:
Direct indexing [index] is impossible on types IEnumerable returned by certain methods.
Here is an example with PythonNet3 where we cannot use indexing because the method face.ToProtoType() returns an IEnumerable and not a IList<T>.
Old (or incorrect) code:
New code (Using LINQ or converting):
LINQ
Advice:
When passing a lambda function as a function parameter, it must be explicitly converted, for example to System.Func[<input_type>, <output_type>].
Some extension libraries still don’t work, like DataTableExtensions.
Example of using LINQ extension methods
Last updated