[IronPython] special method equivalents in C#
dinov at exchange.microsoft.com
Mon Aug 14 17:27:09 CEST 2006
You'll need to implement interfaces for most of these.
You can't actually do __getattr__, but you can do __getattribute__. You'll need to implement ICustomAttributes for this and delegate to ReflectedType for any attribute accesses you don't handle (look at the other classes that implement ICustomAttributes to see how you call ReflectedType).
For __call__ you'll want to implement ICallable.
For __contains__ you'll want to implement IPythonContainer or IMapping. You can also return a zero or non-zero length from GetLength on IPythonContainer and we'll treat that as being equivalent to a value from __nonzero__. We have no special interface for __nonzero__.
In general if you want to find other functionality looking in IronPython\Runtime\Interfaces.cs will give you a quick overview of what's available.
Also, there's a little more complexity on the ones you mentioned. __new__ corresponds more closely w/ a ctor, and __init__ really doesn't have any C# equivalent. Sometimes in the runtime we have both Initialize & static MakeNew functions decorated w/ PythonName's of __init__ and __new__ when we need to use both to match Python semantics.
__hash__ likewise can be interesting because Python's semantics are usually value hashing, whereas .NET usually hashes the reference. We also have a IRichEquality interface which includes RichGetHashCode which returns a value-based hash code instead. This allows us to follow the .NET rule of GetHashCode never throws, while also supporting the Python semantics of things like list and set throwing because they're mutable.
Finally, I would have expected defining __getattr__ to have just worked (and in general all of these should work w/o the interfaces, they'll just be slower). Did you define it on a base type and derive from it? The reason I ask is you could be hitting a problem w/ visibility in the right context. One way to test this would be to put a PythonName("__getattr__") on the method which will make it visible to all Python callers, not just ones who have imported a .NET namespace or clr.
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Christian Schmidt
Sent: Thursday, August 10, 2006 5:02 AM
To: users at lists.ironpython.com
Subject: [IronPython] special method equivalents in C#
I want to implement special methods of classes in C# that I use from ironpython.
For example C#'s equivalent of Python's __iter__ is to derive from IEnumerable, __getitem__ corresponds to this[object], __init__ and __del__ to constructor and destructor, __str__ to toString(), __hash__ to getHashCode(), ...
Is there a table of all conversion from C# to Python special methods or a general mechanism?
Especially I'm looking for the equivalent of __getattr__. I tried __getattr__ in C#, but it didn't get exported in IronPython (atleast it wasn't shown in dir(myclass).
Further methods I'm looking for is __nonzero__, __call__, __contains__.
Currently my workaround is to encapsulate the C#-classes in Python classes extended by the special methods.
Thanks for any hints.
users mailing list
users at lists.ironpython.com
More information about the Ironpython-users