2017-08-06 42 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

謝謝你,你的回答完全解決了我的問題 –