Compare Python (5): ironpython references. Net class

Source: Internet
Author: User
Tags mscorlib python list
Direct import: (limited to all namespaces and classes in mscorlib. dll)

Ironpython can easily use many. Net classes without explicitly adding references.
For example, use from system. CollectionsImport* We can add a reference to the. NET collection class.

 
From system. CollectionsImport* List1 = arraylist () list1.add (1) list1.add (2) list1.add (3)PrintList. countht = hashtable () Ht. Add (1, 1) Ht. Add ('2', 2)

Print ('aaa ')
 
Add references first, and then import
 
All namespaces and classes in mscorlib. dll can be directly imported using the preceding method, because mscorlib. dll is referenced by default.
(Ipy.exe-X: saveassemblies: You can generateProgramWhen the set is cached, you can view it using the decompilation tool.) But if you need to introduce classes in other modules,
You need to add a series of reference methods provided by the CLR module, as if you need to add reference before writing a. Net program,
For example, if you want to use the class in the system. xml namespace
ImportClrclr. addreference ("System. xml") From system. xmlImport* Doc = xmldocument () Doc. Load ("Test. xml")

You can use the decompilation tool to view the generated Assembly. We can see that the Assembly does not add references. in the program, we can see that the reference is added dynamically through reflection.

Interesting quote Phenomena

There are also interesting phenomena when referencing.. Net class will be added later, and the python object will have many more methods and attributes, that is, the python object will have a lot. NET attributes and Methods. Let's take a look at this phenomenon. First, I created a list and then showed all the methods of the initial list through the Dir method. Then, we import CLR, or import system to display all the attributes and methods of the previously created list object.

 
A = [1, 2, 3] method = Dir ()Print'BeforeImport'PrintMethodImportClrmethod = Dir ()Print'AfterImport'PrintMethod

The output is

Before Import
['_ Add _', '_ class _', '_ ins INS _', '_ delitem __', '_ delslice _', '_ Doc _', '_ eq _', '_ getitem _', '_ getslice __', '_ Hash _', '_ iadd _', '_ imul _', '_ init _', '_ ITER __', '_ Len _', '_ module _', '_ Mul _', '_ ne _', '_ new __', '_ reduce _', '_ performance_ex _', '_ repr _', '_ rmul _', '_ setitem __', '_ setSlice _', '_ STR _', 'append', 'Count', 'extend', 'index', 'insert', 'pop ', 'delete', 'reverse', 'sort ']
After import
['Add', 'addsequence ', 'append', 'clear', 'compareto', 'ins insvalue ', 'copyto', 'Count ', 'deleteitem', 'deleteslice ', 'eques', 'extend', 'finalize', 'getdynamictype', 'getnumerator', 'gethashcode', 'getlength', 'getobjectarray ', 'getslice ', 'gettype', 'inplaceadd', 'placemultiply', 'index', 'indexof', 'initialize', 'insert', 'isfixedsize', 'isreadonly ', 'issynchronized', 'handler', 'make', 'makedynamictype', 'makeemptylist', 'makelist', 'memberwiseclone ', 'multiplysequence', 'pop', 'reduce ', 'referenceeques', 'delete', 'removeat', 'reverse', 'reversemultiply', 'richequals ', 'richgethashcode', 'richnotequals', 'setslice ', 'sort ', 'syncroot', 'tocodestring', 'tostring', '_ add _', '_ class _', '_ ins INS __', '_ delitem _', '_ delslice _', '_ Doc _', '_ eq _', '_ getitem __', '_ getslice _', '_ Hash _', '_ iadd _', '_ imul _', '_ init __', '_ ITER _', '_ Len _', '_ module _', '_ Mul _', '_ ne __', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr _', '_ rmul __', '_ setitem _', '_ setSlice _', '_ STR _', 'append', 'Count', 'extend', 'index ', 'insert', 'pop', 'delete', 'failse', 'sort ']

Through comparison, we can find that in the import. after the namespace or class of. net, many methods and attributes starting with an uppercase letter are added to the list. This may be used to facilitate Python objects and. net object interaction. For example, the following statement is used to initialize an arraylist.

 
List = arraylist ([1, 2, 3]) Len (list)
 
We can see that the length of the list is three. That is to say, by assigning a python list to the arraylist constructor, we create an arraylist. We know that
The arraylist constructor can take an icollection as a parameter to initialize arraylist. This indicates that the List class built in Python must be inherited from icollection,
Then he must have the methods provided by icollection. Then, when the. NET namespace or class is not introduced, we do not see these methods using the Dir command,
These methods are not necessary at this time, and the built-in list method is enough. However, once a reference occurs, the interoperability like just now may happen,
Therefore, these methods must be displayed and ready to be called.
 
Differences between import and using
 
1) import can introduce a class, such as import system. Collections. arraylist, while using can only reference namespaces.
 
2) After the import introduces a class, we still need to use it with the full name of the namespace. For example, we want to use it after using the import system. Collections. arraylist
Arraylist, which must be used in this way
 
Arraylist = system. Collections. arraylist ()
An error occurs when writing arraylist = arraylist () directly. Once using references a namespace, you can directly use this class without adding the namespace prefix.
To use these classes conveniently, use from... import...
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.