Ei vielä käännetty
Tätä sivua ei ole vielä käännetty suomeksi, joten se näytetään englanniksi. Auta kääntämään
IronPython Implementation Details¶
IronPython is an implementation of Python that runs on the .NET framework, enabling integration with C# and the .NET ecosystem.
Overview¶
- Platform: .NET Framework / .NET Core
- Use Case: .NET ecosystem integration
- Performance: Good, with .NET runtime optimization
- Compatibility: Python 2.7 (current), Python 3.x planned
Architecture¶
.NET-Based Execution¶
# IronPython code can use .NET classes directly
from System import String
from System.Collections.Generic import List
# Create .NET list
dotnet_list = List[int]()
dotnet_list.Add(1)
dotnet_list.Add(2)
# Behaves like Python but backed by .NET List<T>
Data Structure Implementation¶
List vs .NET List¶
# Python list in IronPython backed by .NET List<T>
my_list = [1, 2, 3]
# Characteristics:
# append: O(1) amortized (like .NET List<T>)
# access: O(1) (like .NET List<T>)
# insert: O(n) (like .NET List<T>)
Dict vs .NET Dictionary¶
# Python dict backed by .NET Dictionary
my_dict = {'key': 'value'}
# Characteristics:
# lookup: O(1) average
# insertion: O(1) average
# Uses .NET hash function
Performance Characteristics¶
Startup¶
Like other .NET applications:
# Startup times
CPython: ~50-100ms
PyPy: ~200-500ms
Jython: ~1-3 seconds
IronPython: ~500ms-1 second (.NET Framework/Runtime load)
# After initial load, fast execution
Runtime Performance¶
.NET JIT compilation:
# Warm-up period like other JIT systems
# Initial runs interpreted
# Hot paths compiled to machine code
def compute(n):
total = 0
for i in range(n):
total += i
return total
# First few calls: interpreted
# Later calls: .NET JIT compiled and fast
.NET Integration Benefits¶
Using .NET Libraries¶
# Direct access to .NET libraries
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Application, Form, Button
# Create Windows Forms GUI in Python!
class MyForm(Form):
def __init__(self):
self.Text = "Python on .NET"
self.button = Button()
self.button.Text = "Click me"
Calling C# Code¶
# Import C# assembly
import clr
clr.AddReference("MyLibrary.dll")
from MyLibrary import MyClass
obj = MyClass()
result = obj.SomeMethod()
Cross-Platform Considerations¶
.NET Framework vs .NET Core¶
.NET Framework (Windows-only):
- Full library access
- Mature ecosystem
- Windows dependent
.NET Core (Cross-platform):
- Windows, Linux, macOS
- Modern runtime
- Growing ecosystem
Comparison with CPython¶
| Feature | CPython | IronPython |
|---|---|---|
| Startup | Fast | Moderate (load .NET) |
| Loops | Interpreted | JIT compiled |
| C Extensions | Yes | No |
| .NET Integration | No | Yes |
| Platform | All | Windows/Linux/Mac (via .NET Core) |
When IronPython Excels¶
.NET Ecosystem Integration¶
# Use .NET libraries from Python
# Examples: WPF, ASP.NET, Entity Framework
# Much more seamless than CPython
# No need for P/Invoke or ctypes
Windows Desktop Applications¶
# Develop Windows Forms or WPF apps in Python
# Direct integration with .NET UI frameworks
Enterprise .NET Environments¶
# Use Python in environments with .NET investment
# Leverage existing C# libraries
# Share code between Python and C#
When CPython is Better¶
Cross-Platform¶
# CPython available on all major platforms
# IronPython requires .NET (Windows, or .NET Core)
Data Science¶
# NumPy, pandas, scikit-learn, etc. require CPython
# No viable alternatives on .NET platform
Standard Environment¶
# CPython is standard
# Most tutorials, packages, tools assume CPython
Practical Considerations¶
Installation¶
# Install from:
# - Windows: Package manager or direct download
# - .NET Core: Cross-platform
ipy --version # Run IronPython
Running Programs¶
# Run Python script on IronPython
ipy my_script.py
# Access .NET from Python
import clr
Current Status¶
| Feature | Status | Notes |
|---|---|---|
| Python 2.7 | Complete | Fully supported |
| Python 3.x | Planned | In development, not yet released |
| .NET Framework | Supported | Windows only |
| .NET Core | Supported | Cross-platform |
| Performance | Good | JIT compilation after warm-up |
Complexity Characteristics¶
Standard operations have same complexity as .NET collections:
| Operation | Complexity | Notes |
|---|---|---|
| List append | O(1) amortized | Amortized; resizing on overflow |
| Dict lookup | O(1) avg, O(n) worst | Hash collisions cause O(n) |
| Set membership | O(1) avg, O(n) worst | Hash collisions cause O(n) |
| Insert at index | O(n) | Requires shifting |
Similar to CPython, but backed by .NET types.
Performance Comparison Example¶
# IronPython performance characteristics
# After JIT warm-up
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# First call: slow (interpretation)
result = fibonacci(20)
# Calls 2-100: Profiling
for _ in range(100):
result = fibonacci(20)
# Call 101+: JIT compiled (much faster than CPython)