[IronPython] Determine the classes/interfaces a Python implements

Dino Viehland dinov at microsoft.com
Mon Dec 15 22:29:15 CET 2008

You should call PythonOps.IsSubClass and pass in the PythonType and the interface you want to compare it with.  The interface can be either a .NET type object or another Python type object (or a tuple, etc...)

The not raising on missing functions is a feature :)  You could build a meta-class which would look at the incoming arguments and validate that all of the methods in an interface are implemented.  That shouldn't be too hard - probably just seomthing like:

assert (set(dir(someInterface)) & set(newTypeDict)) == set(dir(someInterface))

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeff Slutter
Sent: Monday, December 15, 2008 12:15 PM
To: Discussion of IronPython
Subject: [IronPython] Determine the classes/interfaces a Python implements

I have a Python script that creates a class within it. This Python class
is derived off of a class, or interface, I made in C# - something like:

class MyClass(Test.MainForm.IScript):

Now, back in C#, I have gotten access to "MyClass" by:

object myclass = someScope.GetVariable("MyClass");

Is there a way to determine either:
a) what classes/interfaces MyClass implements OR
b) if it implements a specific class/interface

I want to know if the "object myclass" is supposed to implement
Test.MainForm.IScript or not.

I don't want to create an instance of MyClass as this would cause
problems, executing things I'm not ready to execute.

Also, related but not as important, implementing an interface (as above)
doesn't cause any compiler errors if I'm missing functions - is there a
way to enforce this?

Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list