[IronPython] clr.ImportExtensions are not available outside module ?

Dino Viehland dinov at microsoft.com
Thu May 12 10:01:33 PDT 2011



Daniel wrote:
> Just as Dave Wald, I have also trying out the ImportExtensions methods
> which I personally find very useful, thank you for implementing. Everything is
> working fine on using 2.7 (and .net 4), but extension methods loaded by one
> module seem not available on another...

This is by design.  It works similar to how "import clr" only effects the current module
and how extension methods work in C# where they are scoped by file.  So if you
want extension methods available in a file you'll need to import them in that file. 
If they were globally scoped it would be easy for one module to break another by
bringing in the 'wrong' extension methods.

> 
> 
> I will stick to Dave's code exmaple:
> 
> import clr
> clr.AddReference("System.Core")
> import System
> from System import Linq
> 
> clr.ImportExtensions(Linq)
> 
> class Product(object):
>      def __init__(self, cat, id, qtyOnHand ):
>          self.Cat = cat
>          self.ID = id
>          self.QtyOnHand = qtyOnHand
>          self.Q = self.QtyOnHand
> 
> If I put this into a file, lets say test.py and now use the module on the
> command line:
> 
> >>> import test
> >>> products = [test.Product('food', i, 10) for i in range(3)]
> >>>
> >>>
> >>> products.Where
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'list' object has no attribute 'Where'
> 
> The linq extensions are not available. I have to re-import them:
> 
> >>> test.clr.ImportExtensions(test.Linq)
> >>> products.Where
> <built-in method Where of list object at 0x000000000000002B>
> >>>
> 
> Is this a current limitation, or should I be doing things differently ?
> Any help appreciated.
> 
> Greetings,
> Daniel
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


More information about the Users mailing list