Python for ios解释器 [英] Python for ios interpreter

查看:387
本文介绍了Python for ios解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

iOS上的Python或Ruby解释器

我只是发现这个应用程序 pypad python for ios

I just discovered this apps pypad and python for ios

他们喜欢翻译编辑

那么你会推荐哪个app

So which app would you recomend

但最重要的是,这个解释器是如何工作的,我在哪里可以看到一个如何使用obj c的例子和python开始工作togheter?

But most importantly, how does this interpreter work, and where can i see an example of how the obj c and python get to work togheter?

谢谢!

推荐答案

我我是适用于iOS的Python 的唯一创建者,所以这当然是我推荐的,但是个人决定的一个很好的指标是评论&安培;每个应用程序的评级。我花了几周的时间才弄清楚如何将python正确地集成到这个App的Objective-c中,但这里是让你入门的最佳资源(请记住,ObjC只是C的超集):

I am the sole creator of Python for iOS so that is of course what I would recommend, but a good indicator for your personal decision is the reviews & ratings of each App. It took me weeks to figure out how to properly integrate python into Objective-c for this App but here is the best resource to get you started (keep in mind that ObjC is just a superset of C):

http://docs.python.org/c-api/

此外,这是一个调用 myModule 。等价的python将是:

Also, here is an example of calling a function defined in myModule. The equivient python would be:

import myModule
pValue = myModule.doSomething()
print pValue

在Objective-c中:

In Objective-c:

#include <Python.h>

- (void)example {

    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
    NSString *nsString;

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyString_FromString("myModule");

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, "doSomething");

    if (PyCallable_Check(pFunc)) {
        pValue = PyObject_CallObject(pFunc, NULL);
        if (pValue != NULL) {
            if (PyObject_IsInstance(pValue, (PyObject *)&PyUnicode_Type)) {
                    nsString = [NSString stringWithCharacters:((PyUnicodeObject *)pValue)->str length:((PyUnicodeObject *) pValue)->length];
            } else if (PyObject_IsInstance(pValue, (PyObject *)&PyBytes_Type)) {
                    nsString = [NSString stringWithUTF8String:((PyBytesObject *)pValue)->ob_sval];
            } else {
                    /* Handle a return value that is neither a PyUnicode_Type nor a PyBytes_Type */
            }
            Py_XDECREF(pValue);
        } else {
            PyErr_Print();
        }
    } else {
        PyErr_Print();
    }

    // Clean up
    Py_XDECREF(pModule);
    Py_XDECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    NSLog(@"%@", nsString);
}

有关更多文档,请查看:扩展和嵌入Python解释器

For much more documentation check out: Extending and Embedding the Python Interpreter

这篇关于Python for ios解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆