武夷山建设局网站,微信小程序怎么添加,辽宁建设银行官方网站,asp网站和php网站声明#xff1a;本文做为IronPython-2.0 B3的Tutorial 中文译文#xff0c;内容全部来自其英文原文#xff0c;其中本人认为存在疑问的或翻译不当之处会用原文中的内容加以标记#xff0c;且本文内容完全用于研 究和学习IronPython 之用#xff0c;限于本人英文翻译功底有… 声明本文做为IronPython-2.0 B3的Tutorial 中文译文内容全部来自其英文原文其中本人认为存在疑问的或翻译不当之处会用原文中的内容加以标记且本文内容完全用于研 究和学习IronPython 之用限于本人英文翻译功底有限如有错误欢迎大家批评指正。 原文链接如下http://www.cnblogs.com/daizhj/articles/1245093.html Introduction IronPython 作为 Python 语言在.net平台上的实现(www.python.org)它是一种动态 语言且支持许多编程范例[paradigms] 诸如面向对象也允许您使用.net代码进行编译。 本tutorial的目标就是使您快速熟悉IronPython 控制台console并向您展示如何有效的使用扩展extensive.net库。这个 tutorial同时还会展示如何从一些特殊领域入手 这些领域诸如COM的内部操作用C#扩展 IronPython 以及嵌入式的IronPython 等。当 然本tutorial决不是针对Python语言自身进行介绍如果您想查阅这方面的资料我们推荐您 去www.python.org 或 看一下由Mark Lutz 与 David Ascher 合著的“Learning Python” 这本书。 本tutorial所需要的运行环境如下 Microsoft .NET Framework Version 2.0 Redistributable Package (x86) Required to run IronPython. Download from here.译者注本人机器上是.net3.5也可运行部分的示例所以大家不必非要下载2.0.NET Framework 2.0 Software Development Kit (SDK) (x86) Required for the COM interoperability, extending and embedding tutorials. Download from here. Microsoft WinFX Runtime Components (Avalon) Required for Advanced IronPython and Embedding IronPython tutorials. Download from here. Mapack (example assembly found on the internet) Required for the Basic IronPython tutorial, exercise Loading .NET Libraries. Download Mapack from here (direct link to the Mapack.zip download is here). Extract Mapack.dll from the zip file directly into the Tutorial directory. 译者注:如果您本机上安装的是vs2008 RTM笔者的开发环境,那么你就可以跳过上面的几个安装 直接进入到下面的正文了而如果您安装的是vs2005那么请您检查相应的条件是否已不足。 本tutorial假设IronPython的发布包已被解压缩至C:\IronPython. 请注意您个人的安装可能会存在 一定的差异。 本tutorial假设您将会使用IronPython 控制台功能。将tutorial指导您从tutorial目录启动控制台程 序时您应该定位到tutorial目录下(cd c:\ironpython\tutorial) 并运行控制台程序(..\ipy.exe)。 译者注ipy.exe可以下面链接中直接下载我这样做主要是觉得园子里有些朋友可以只是想了解并试 用一下并不想下载那个IronPython的源码包或相应的sdk等资源。 好了让我们开始正文吧。 Tutorial 1: 基本IronPython应用 本章节主要强调的是IronPython的interpreter 以及与.NET 类库的互动场景environment 本章节阅读大概需要30分钟 The objective of this tutorial is to launch the IronPython interpreter, explore the environment of the interactive console and use IronPython to interact with .NET libraries. The exercises in this tutorial are: The IronPython interactive console Using the standard .NET libraries from IronPython Loading additional .NET libraries 练习 1: IronPython 交互控件台 In this exercise, you will start the IronPython interactive interpreter and perform simple tasks to become acquainted with the IronPython environment. If you are familiar with using the Python interactive console, the import statement and exploring the Python interactive environment using dir() function and __doc__ attribute, you can skip this exercise. 任务 1: IronPython console 从本Tutorial的路径下启动IronPython consolec:ironpythonipy.exe。它将如在DOS窗口 下出现如下提示 IronPython 1.0 on .NET 2.0.50727.42 Copyright (c) Microsoft Corporation. All rights reserved. _ 运行下面的内容在每一条内容之后按下回车键都会打印prints一条结果。 22 print Hello World! for i in range(3): print i x 10 print x 运行完上面操作后控制台窗口将会显示如下内容 22 4 print Hello World! Hello World! for i in range(3): print i ... 0 1 2 x 10 print x 10 IronPython 控制台支持多行声明multi-line statements这经常要进行函数定义: ... Unlike C# or Java, where blocks of code are grouped by curly brackets {...}, blocks of code in Python are grouped based on their level of indentation. Every new block of code must be indented one more level than the previous block of code. Blocks of code are used for function and class definitions as well as if statements and loops. 例如定义“add”方法注你需要在return 之前敲入空格符: return a b 要完成函数定义按下Enter键。 add(3, 2) add(Iron, Python) 完成这一步后控制台内容如下 def add(a, b): ... return a b ... add(3, 2) 5 add(Iron, Python) IronPython 要退出控制台程序按下CtrlZ 并回车即可(alternatively, press F6 followed by Enter). ^Z Task 2: 内置模块和交互式探查(interactive exploration) 从tutorial路径启动IronPython 控制台 (see Introduction for details). 使用内置dir() 函数, 列出IronPython 环境的内容: dir() 控制台显示如下: dir() [__builtins__, __doc__, __name__] IronPython 有几个内置模块经常使用的是sys. 引入sys 模块使用 import 声明: import sys Python 引入声明与 C# 的 using 或Visual Basic的Imports声明相似。 The important difference is that the C# and VB statements bring the names from the imported namespace into the global namespace to be accessed directly. Python’s import doesn’t do that. 要访问引入模块的名称和属性, 使用module名称的前缀: sys.version 使用dir()方法去显示环境: dir() 改变环境 (global namespace), 现在它包括sys 模块: dir() [__builtins__, __doc__, __name__, sys] 使用dir() 方法来显示sys 模块的内容: dir(sys) 打印出 sys 模块属性的一些值 sys.path sys.executable Task 3: 外部 Python 模块 本任务使用 first.py 文件模块位于Tutorial 路径. 声明 first.py 模块您可以运行在Tutorial 路径下的ipy.exe“first”将位于sys.path路径下: import first 用dir()函数显示first 模块: dir(first) dir(first) [__builtins__, __file__, __name__, add, factorial, hi] 为打印 add 和 factorial 函数文档内容使用__doc__属性 first.add.__doc__ first.factorial.__doc__ __doc__属性可用于显示 .NET 方法和它们的参数类型。 first.add.__doc__ add(a, b) - returns a b first.factorial.__doc__ factorial(n) - returns factorial of n 调用 first 模块方法并打印“hi”属性内容 first.add(1,2) first.factorial(5) first.hi 输出内容为 first.add(1,2) 3 first.factorial(5) 120 first.hi Hello from IronPython! 在控制台下CtrlZ 或 F6 按回车退出。 Exercise 2: 使用标准.NET 库 IronPython 的能力在于它可以无缝访问.NET 类库。这个练习将会示范如何使用标准.NET 类库。 Task 1: 基本.NET 类库使用 在tutorial路径下启动IronPython 控制台 (see Introduction for details). 使用 import 声明, 引入 .NET 系统名空间: import System 显示System.Environment 类并访问其下面的属性: dir(System.Environment) System.Environment.OSVersion System.Environment.CommandLine 如下是其命令行的输入 dir(System.Environment) System.Environment.OSVersion System.OperatingSystem object at 0x000000000000002B [Microsoft Windows NT 6.0.6000.0] System.Environment.CommandLine C:IronPythonipy.exe 使用“from ... import ... 引入相应类库声明并使用dir()显示全局名空间内容。 from System.Math import * dir() 现在您可以调用Math 方法而不必指定名空间和类前缀 Sin(PI/2) 显示输出为: from System.Math import * dir() [Abs, Acos, Asin, Atan, Atan2, BigMul, Ceiling, Cos, Cosh, DivRem, E, Equals, Exp, Floor, GetHashCode, GetType, IEEERemainder, Log, Log10, Max, Min, PI, Pow, Round, Sign, Sin, Sinh, Sqrt, System, Tan, Tanh, ToString, Truncate, __builtins__, __doc__, __name__] Sin(PI/2) 1.0 Task 2: 使用.NET 类 声明“System.Collections”名空间到“全局名空间”内 from System.Collections import * 创建 Hashtable 类实例并使用dir()显示实例的内容 h Hashtable() dir(h) 在hash表中插入一些元素: h[a] IronPython h[b] Tutorial IronPython 支持C# 风格语法来访问hash表元素. 相同的语法可用于支持索引的对象(Arrays, Array lists etc): h[a] 上面的操作步骤将会显示如下输出: h[a] IronPython h[b] Tutorial h[a] IronPython 使用声明for ... in ... 列举hash表的内容. hash 表元素作为DictionaryEntry 类实例. 打印Key和Value属性: for e in h: print e.Key, :, e.Value 控制台显示如下输入: for e in h: print e.Key, :, e.Value ... a : IronPython b : Tutorial 你可以向python内置列表传递参数来初始化集合类. 您可以通过[1,2,3]来创建指定元素来创 建Python 列表[1,2,3]. You create tuples by specifying elements in the parentheses: (1,2,3). l ArrayList([1,2,3]) for i in l: print i s Stack((1,2,3)) while s.Count: s.Pop() 输出如下: l ArrayList([1,2,3]) for i in l: print i ... 1 2 3 s Stack((1,2,3)) while s.Count: s.Pop() ... 3 2 1 Task 3: 泛型 从System.Collections.Generic名空间中来声明泛型集合类: from System.Collections.Generic import * 初始化泛型类实例, 其泛型类型参数必须被指定. IronPython 使用如下语法来指定类型参数: generic_type[type_argument, ...] 泛型类型[类型参数]. 创建泛型string列表实例: l List[str]() 添加字符串数据到列表中 因为创建了字符串列表可以进行添加: l.Add(Hello) l.Add(Hi) 尝试添加其它类型的数据: l.Add(3) l.Add(2.5) l.Add([1,2,3]) 显然添加“非字符串”型数据会导致如下“类型错误”输出: l.Add(3) Traceback (most recent call last): File , line unknown, in Initialize##40 TypeError: expected str, got int 遍历泛型集合: for i in l: print i The output will be: for i in l: print i ... Hello Hi TypeError: expected str, got int TypeError: expected str, got float TypeError: expected str, got list 按下CtrlZ 或 F6 回车退出IronPython 控制台。 好了今天就先翻译到这里了看了看原文发现刚翻译了1/4, 看来要再用2-3个篇幅来完成全篇翻译。我 想等全部翻译结束后将翻译的各篇文章拼揍起来。 感兴趣的朋友可以通过EMAIL或在回复中与我联系。 控制台程序下载链接http://www.cnblogs.com/files/daizhj/ipy.zip 转载于:https://www.cnblogs.com/daizhj/archive/2008/07/21/1247345.html