. NetProgramMembers are happy, basically spoiled by ms, and bosses are happy, but as a programmer, they are not happy (for some reason, I think ). With the Open Source spring breeze of MS, let's see how Ms spoiled our cute. Net programmers. What rules should Ms follow when designing products and how to solve some difficult problems. Let's take a look at Microsoft's fully open-source ironpython and how Ms plays a fortune.
Microsoft is now a member of the python Software Foundation (PSF) (similar to that of Microsoft when joining the Java Team). ironpython must be compatible with cpython to take care of the original pythoner, however, ironpython is built on. net Framework. net programmer. There must be a conflict between the two groups. How does Microsoft maintain a balance between the two groups? Let's take a few simple examples to see the beauty of ironpyhon's design.
>>>S= "Python and. Networking together"
A seemingly simple string processing problem:
>>> S = " Python and. Networking together "
>>> S. Upper ()
>>>S. Upper ()
In python, the upper function is used to obtain uppercase strings, but in. net, the string. toupper () method is used. If it was you, what would you do at this time? Maybe you would think of the. NET Extension Method to handle it like this:
>>> S = " Python and. Networking together "
>>> S. Upper ()
' Python and. Net working together '
>>> S. toupper ()
' Python and. Net working together '
In this case, the original pythoner will yell, and they require compatibility. cpython does not support the toupper () method. Let's think about how to solve this problem. I just thought of the extension method. Well, we can handle it like this:
>>> S = " Python and. Networking together "
>>> S. Upper ()
' Python and. Networking together '
>>> S. toupper ()
Traceback (most recent call last ):
File " <Stdin> " , Line 1 , In < Module >
Attributeerror: ' Str ' Object has no attribute ' Toupper '
>>> Import CLR
>>> S. toupper ()
' Python and. Networking together '
>>>
Now everyone is satisfied. Similarly, we can solve to_upper in ironruby; toupper, toupper, and toupper in VB.
Here is an example of exception handling: >>> Import System
>>> System. Math. divrem ( 5 , 2 )
( 2 , 1 )
>>> System. Math. divrem ( 5 , 0)
Traceback (most recent call last ):
File " <Stdin> " , Line 1 , In < Module >
Zerodivisionerror: try to divide by zero.
Have you seen the problem? Careful. Net programmers will call out: isn't the dividebyzeroexception thrown in C? The same error in ironpython throws the zerodivisionerror exception. Well, it seems like a problem. Let's see how ironpython handles this problem: >>> Try :
System. Math. divrem ( 5 , 0)
Except Zerodivisionerror, E:
Print E, type (E)
Try to divide by zero. < Type ' Exceptions. zerodivisionerror ' >
Pythoner does capture the desired exception, so it takes good care of the original pythoner. For. Net programmers, can they catch the dividebyzeroexception? do an experiment: >>> Try :
System. Math. divrem ( 5 , 0)
Except System. dividebyzeroexception, E:
Print E, type (E)
System. dividebyzeroexception: try to divide by zero.
In _ stub _ $ 17 # #16 (closure, callsite, codecontext, object, int32, int32)
In _ stub_matchcaller (object, callsite, object [])
In Microsoft. Scripting. Actions. callsite' 1 . Updateandexecute (object [] ARGs)
In Microsoft. Scripting. Actions. updatedelegates. update4 [T, T0, T1, T2, T3, Tret] (CA
Llsite site, t0 arg0, T1 arg1, T2 arg2, T3 arg3)
In initialize $ 27 # #26 (closure, codecontext) <type 'didbyzeroexception'>
It is also very good and powerful. Net dividebyzeroexception is also caught. Someone has to ask: what type is this exception? Try again: >>> Import System
>>> Try :
System. Math. divrem ( 5 , 0)
Except System. exception, E:
Print Type (E)
< Type ' Dividebyzeroexception ' >
>>>
Or dividebyzeroexception in. net.