[IronPython] Some thoughts on namespaces, extension methods

Dino Viehland dinov at microsoft.com
Wed Nov 12 23:19:19 CET 2008


Actually I'd say modifying built-in types is unpythonic - for example you can't do object.xyz = 42.  We could choose to break this rule for types we don't share between Python & .NET, and it probably wouldn't be a lot of work, but I'm hesitant to have the two sets of types treated differently.

I do have a lot of sympathy for being able to modify the namespaces (and maybe static types) though which are more like modules - and that would be Pythonic.  It's interesting that this is the 2nd time in the past week or so that it's come up.  So I'd say feel free to file a feature request for modifying namespaces if you think that alone would be as useful.  But unless there was overwhelming demand for monkey patching .NET types and no disagreement I think that won't happen.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dan Eloff
Sent: Wednesday, November 12, 2008 2:12 PM
To: Discussion of IronPython
Subject: [IronPython] Some thoughts on namespaces, extension methods

I was doing some cross WPF/Silverlight work, and invariably I had to
pepper the code with if sys.platform == 'silverlight' use silverlight
api, else use different wpf api for the same task. That approach goes
back to C and beyond, and is unpythonic. Refactoring to create a
common api for dealing with such things works, but mostly just moves
the ugliness into one file.

It occurs to me that in all of these cases I want to access either a
silverlight or wpf api unmodified, and simulate it on the other
platform. The most pythonic way to resolve this would be if namespaces
and CLR classes could be modified. Mutable namespaces and support for
extension methods would allow most of this. I think that in C# there
is no such thing as static extension methods, but it may be possible
that the CLR allows this. Anyway, I do not know how difficult or
practical this is, but it is certainly pythonic.

-Dan

if sys.platform == 'silverlight':
    def read_all_bytes(path):
        ...
    File.ReadAllBytes = read_all_bytes

if sys.platform == 'cli':
    def load_xaml_from_string(s):
        ...
    XamlReader.LoadXaml = load_xaml_from_string
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com




More information about the Ironpython-users mailing list