Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created October 22, 2009 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschementi/215819 to your computer and use it in GitHub Desktop.
Save jschementi/215819 to your computer and use it in GitHub Desktop.
IronPython and C# 4 "dynamic"
using System;
using IronPython.Hosting;
public class dynamic_demo {
static void Main() {
var ipy = Python.CreateRuntime();
dynamic mock = ipy.UseFile("mock.py");
// Calls the Python Mock type's constructor
dynamic m = mock.Mock();
//The Python Mock type dynamically implements any member that is requested of it
System.Console.WriteLine(
m.the_csharp_compiler_cannot_possbily_know_this_member_exists_at_compile_time
);
}
}
import random, math
class Mock(object):
def __getattr__(self, key):
"""Mock objects of this type will dynamically implement any requested member"""
return random.choice(["hello world", math.pi])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment