用 ObjectScript 编写的方法中的参数可以通过值或引用传递。在下面的方法中,签名中第二个和第三个参数前面的 ByRef 关键字表示它们打算通过引用传递。
ClassMethod SandwichSwitch(bread As %String, ByRef filling1 As %String, ByRef filling2 As %String)
{
set bread = "whole wheat"
set filling1 = "almond butter"
set filling2 = "cherry preserves"
}从 ObjectScript 调用方法时,在参数前放置一个句点以通过引用传递它,如下所示:
USER>set arg1 = "white bread"
USER>set arg2 = "peanut butter"
USER>set arg3 = "grape jelly"
USER>do ##class(User.EmbeddedPython).SandwichSwitch(arg1, .arg2, .arg3)
USER>write arg1
white bread
USER>write arg2
almond butter
USER>write arg3
cherry preserves从输出中可以看出,调用 SandwichSwitch() 后变量 arg1 的值保持不变,而变量 arg2 和 arg3 的值发生了变化。
由于 Python 本身不支持按引用调用,因此需要使用 iris.ref() 方法创建一个引用,以将每个要按引用传递的参数传递给该方法:
>>> import iris
>>> arg1 = "white bread"
>>> arg2 = iris.ref("peanut butter")
>>> arg3 = iris.ref("grape jelly")
>>> iris.cls('User.EmbeddedPython').SandwichSwitch(arg1, arg2, arg3)
>>> arg1
'white bread'
>>> arg2.value
'almond butter'
>>> arg3.value
'cherry preserves'可以使用 value 属性访问 arg2 和 arg3 的值,并查看它们在调用该方法后发生了变化。
%SYS.Python 类具有 True()、False() 和 None() 方法,它们分别表示 Python 标识符 True、False 和 None。
例如:
USER>zwrite ##class(%SYS.Python).True()
2@%SYS.Python ; True ; <OREF>如果需要将 True、False 和 None 传递给 Python 方法,这些方法很有用。以下示例使用关键字或命名参数中显示的方法。
USER>do obj.mymethod(##class(%SYS.Python).True(), ##class(%SYS.Python).False(), ##class(%SYS.Python).None())
foo=True, bar=False, baz=None如果将未命名的参数传递给需要关键字参数的 Python 方法,Python 会按照传入的顺序处理它们。
请注意,在检查 Python 方法返回给 ObjectScript 的值时,不需要使用方法 True()、False() 和 None()。
假设Python模块mymodule也有一个方法isgreaterthan(),定义如下:
def isgreaterthan(a, b):
return a > b在 Python 中运行时,可以看到如果参数 a 大于 b,则该方法返回 True,否则返回 False:
>>> mymodule.isgreaterthan(5, 4)
True但是,当从 ObjectScript 调用时,返回值为 1,而不是 Python 标识符 True:
USER>zwrite obj.isgreaterthan(5, 4)
1在 Python 中,字典通常用于以键/值对的形式存储数据,例如:
>>> mycar = {
... "make": "Toyota",
... "model": "RAV4",
... "color": "blue"
... }
>>> print(mycar)
{'make': 'Toyota', 'model': 'RAV4', 'color': 'blue'}
>>> print(mycar["color"])
blue在 ObjectScript 方面,可以使用 Python 内置模块的 dict() 方法来操作 Python 字典:
USER>set mycar = ##class(%SYS.Python).Builtins().dict()
USER>do mycar.setdefault("make", "Toyota")
USER>do mycar.setdefault("model", "RAV4")
USER>do mycar.setdefault("color", "blue")
USER>zwrite mycar
mycar=2@%SYS.Python ; {'make': 'Toyota', 'model': 'RAV4', 'color': 'blue'} ; <OREF>
USER>write mycar."__getitem__"("color")
blue上面的示例使用字典方法 setdefault() 来设置键的值,并使用 __getitem__() 来获取键的值。
在 Python 中,列表存储值的集合,但没有键。列表中的项目通过其索引访问。
>>> fruits = ["apple", "banana", "cherry"]
>>> print(fruits)
['apple', 'banana', 'cherry']
>>> print(fruits[0])
apple在 ObjectScript 中,可以使用 Python 内置模块的 list() 方法处理 Python 列表:
USER>set l = ##class(%SYS.Python).Builtins().list()
USER>do l.append("apple")
USER>do l.append("banana")
USER>do l.append("cherry")
USER>zwrite l
l=13@%SYS.Python ; ['apple', 'banana', 'cherry'] ; <OREF>
USER>write l."__getitem__"(0)
apple上面的示例使用列表方法 append() 将项目附加到列表中,并使用 __getitem__() 获取给定索引处的值。 (Python 列表是从零开始的。)
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。