.NETã¯ã©ã¹ããã®ç¶æ¿
æ¥è¨ã§ä½åº¦ããµãããã¨ãããã¾ãããIronPythonã®ã¯ã©ã¹ã¯.NETã§è¨ãã¨ããã®åã§ã¯ãªããIronPythonç¬èªã®ã¯ã©ã¹ã«ãªã£ã¦ãã¾ã(æããPythonåæ§ã®å®è£ )ã
import sys class Spam: def Hi(self): print 'Spam' class Eggs: def Say(self): print 'Eggs' s = Spam () s.Hi () e = Eggs () e.Say () # Pythonã¯å¤éç¶æ¿ããµãã¼ããã¦ãã class SpamEggs(Spam, Eggs): pass se = SpamEggs () se.Say () se.Hi () # ã¤ã³ã¹ã¿ã³ã¹ã«Nameã¡ã³ãã追å ãã¦ã¿ã se.Name = 'Python' # ã¯ã©ã¹ã«ã¡ã½ããã追å ãã¦ã¿ã SpamEggs.Goodby = lambda self: sys.stdout.write ('Goodby ' + self.Name + '\n') # å¼ã³åºãã¦ã¿ã se.Goodby () # çµæ # Spam # Eggs # Eggs # Spam # Goodby Python
å®è¡æã«ã¡ã³ãã追å ã§ãããã¨ã.NETã®åã«ã¯ç¡ãæè»æ§ãããã¾ããå¿è«ãããã ãã ã¨.NETã©ã¤ãã©ãªãå¼ã¹ãã ãã®Pythonã«éãã¾ãããIronPythonã§ã¯.NETã®åããç¶æ¿ãã¦Pythonã®ã¯ã©ã¹ãä½æãããã¨ãåºæ¥ã¾ããã¨ãããã¨ã§ãC#ã§ã¢ã»ã³ããªãä½ã£ã¦IronPythonã§ç¶æ¿ãã¦ã¿ã¾ãããã
using System; namespace Spam { public class Foo { public void Say () { Console.WriteLine ("Say Foo."); } } public sealed class SealedFoo { public void Say () { Console.WriteLine ("Say SealedFoo."); } } }
é¢ç½ãããªãã¨ããªãã³ã¼ãã§ããããããã³ã³ãã¤ã«ãã¦Foo.dllã¨ãã¾ããã§ã¯ãIronPythonããå¼ãã§ã¿ããã¨ã«ã
import clr # Foo.dllãåç §ãã clr.AddReference('Foo.dll') # Spamåå空éããå ¨ã¦ã®åãã¤ã³ãã¼ã from Spam import * foo = Foo () foo.Say () sfoo = SealedFoo () sfoo.Say () # .NETåããç¶æ¿å¯è½ class MyFoo (Foo): pass myfoo = MyFoo () myfoo.Say () # sealedåã¯ä¸å¯ # class MySealedFoo (SealedFoo): # pass class Bar: pass # ãã¼ã¹ã¯ã©ã¹ãPythonã¯ã©ã¹ã§ãªããã°å¤éç¶æ¿ã¯ä¸å¯ # ãã®å ´åãFooã.NETåãªã®ã§é§ç® # class FooBar(Foo, Bar): # pass # çµæ # Say Foo. # Say SealedFoo. # Say Foo.
å¤éç¶æ¿ãåºæ¥ãªããªã©ã®å¤å°ã®å¶éãããã¾ãããåé¡ãªã使ãã¾ããã
ã¨ããã§ãä¸ã®ä¾ã«ããããsealedãå ¥ããã®ã«ã¯ããããã£ã¦ã
class MyString(String): pass
ãã®ããã«sealedã§ããã¯ãã®Stringåããç¶æ¿åºæ¥ã¦ãã¾ããã§ãããããã®éããåãããªãã¦èª¿æ»ãã¾ããã
å®ã¯ãStringããç¶æ¿åºæ¥ãããã«è¦ããã®ã§ããStringã®ã¡ã³ãã«ã¢ã¯ã»ã¹ããã¨ã¨ã©ã¼ã«ãªãã¾ããè²ã å¼ã£ã¦ã¿ã¦ãªãã¨ãªãåãã£ããã§ãããçµè¾¼åã¯.NETåã¨Pythonç¬èªåããå¤éç¶æ¿ãããããªåã¿ããã§ããStringåãimportããªããã°ãæåååã¯Pythonæååã¨ãã¦ã®ã¡ã³ãããæã¡ã¾ãããã¨ããããStringåãimportããç¬éã.NETåã®ã¡ã³ããå«ã¾ãã¾ããã§ãå ã»ã©ã®Stringããã®ç¶æ¿ã¯Pythonæåååããã®ç¶æ¿ãæå³ãã¦ãã¦ãPythonæåååã®ã¡ã³ãã«ã¢ã¯ã»ã¹ããåã«ã¯æ£å¸¸ã«åä½ãã¾ãã