[IronPython] Magic methods on CLR types

Dino Viehland dinov at microsoft.com
Thu Dec 3 00:26:29 CET 2009


The reason we don't pick up your __repr__ is we don't expect it to be defined on types which aren't marked w/ PythonTypeAttribute.  Therefore we go w/ our default implementation.  If you put [PythonType] on it we'll pick it up though.  There's 6 other methods which are a little magical like this where we do something special before going to reflection (__str__, __new__, __repr__, __hash__, __iter__, and __reduce_ex__).  Ideally we should probably hit reflection first or make the early resolution something we internally opt-in to.

There's no extensible way to change the type names by extension currently - we just have a hard coded table for the 15 types we rename right now.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeffrey Sax
Sent: Wednesday, December 02, 2009 3:14 PM
To: 'Discussion of IronPython'
Subject: Re: [IronPython] Magic methods on CLR types

That works, even for generic types! Thank you.

I did notice that __repr__ (and possibly others) is not picked up unless I add ExtensionType attributes for each public subtype of a base type that I'm extending. My guess is that IronPython already supplies a default implementation for these, so there is no need to look up in the hierarchy. This is understandable but somewhat unexpected.

Is there a way to assign Python-friendly names to CLR types by extension? I.e. get the same effect as if a PythonTypeAttribute was applied to the original class?

Thanks,

Jeffrey

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Tuesday, December 01, 2009 8:57 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Magic methods on CLR types

If you want IronPython to recognize them you just need to create an assembly w/ ExtensionTypeAttribute and have it point at the extended and extending types.  Then the assembly needs to be loaded into the ScriptRuntime using ScriptRuntime.LoadAssembly.

We still need to figure out our story for recognizing and importing normal .NET extension methods that C# supports.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeffrey Sax
Sent: Tuesday, December 01, 2009 5:27 PM
To: 'Discussion of IronPython'
Subject: [IronPython] Magic methods on CLR types

Is there a way to add magic methods like __repr__, __call__, etc. to CLR types?


1.       Can it be done in the external CLR assembly without polluting the API for other languages? If so, how?

2.       Can it be done using F# style type augmentations by redefining such methods on the Python type corresponding to the CLR type? If so, how?

3.       If neither of these alternatives is possible, is there a third way?

Thanks!

Jeffrey Sax
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091202/ffe9ddef/attachment.html>


More information about the Ironpython-users mailing list