2017-08-06 51 views
-1

PyCall文档说: 重要提示:与Python最大的区别是对象属性/成员是用o [:attribute]而不是o访问的。属性,以便Python中的o.method(...)被替换为Julia中的o:方法。此外,您使用get(o,key)而不是o [key]。 (但是,可以通过O访问整数指数为[I]如在Python,尽管基于1儒略指数而不是0-基于Python指数。)我如何使用python内置函数像isinstance()在茱莉亚使用PyCall

但我不知道哪个模块或对象以导入

+1

使问题更加清晰。如何做到这一点:a)添加一些你的代码。 b)解释你的函数的目的是什么或者它想要计算什么。 c)使您共享的代码可运行,并可能添加一些结果或错误。 d)添加一个是问题的句子(?最后),并试图描述答案是什么。 (a),(b),(c)和(d)的任何组合都会有所帮助。 –

+0

无论如何,谢谢你,下面的第一个答案解决了我的问题。 –

回答

2

这里有一个简单的例子,让你开始

using PyCall 

@pyimport numpy as np   # 'np' becomes a julia module 

a = np.array([[1, 2], [3, 4]]) # access objects directly under a module 
           # (in this case the 'array' function) 
           # using a dot operator directly on the module 
#> 2×2 Array{Int64,2}: 
#> 1 2 
#> 3 4 

a = PyObject(a)     # dear Julia, we appreciate the automatic 
           # convertion back to a julia native type, 
           # but let's get 'a' back in PyObject form 
           # here so we can use one of its methods: 
#> PyObject array([[1, 2], 
#>     [3, 4]]) 

b = a[:mean](axis=1)   # 'a' here is a python Object (not a python 
           # module), so the way to access a method 
           # or object that belongs to it is via the 
           # pythonobject[:method] syntax. 
           # Here we're calling the 'mean' function, 
           # with the appropriate keyword argument 
#> 2-element Array{Float64,1}: 
#> 1.5 
#> 3.5 

pybuiltin(:type)(b)    # Use 'pybuiltin' to use built-in python 
           # commands (i.e. commands that are not 
           # under a module) 
#> PyObject <type 'numpy.ndarray'> 

pybuiltin(:isinstance)(b, np.ndarray) 
#> true 
+0

谢谢你,你的回答完全解决了我的问题 –