[IronPython] Some thoughts on namespaces, extension methods

Dan Eloff dan.eloff at gmail.com
Wed Nov 12 23:12:12 CET 2008


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



More information about the Ironpython-users mailing list