Deutsch English Français Italiano |
<python-20240803162221@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.c Subject: Re: is STC a good supplementary library for C? Date: 3 Aug 2024 15:25:09 GMT Organization: Stefan Ram Lines: 78 Expires: 1 Jul 2025 11:59:58 GMT Message-ID: <python-20240803162221@ram.dialup.fu-berlin.de> References: <j4KdnZzYDexmlDP7nZ2dnZfqn_idnZ2d@brightview.co.uk> <containers-20240803130427@ram.dialup.fu-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de n9jlqbxviRkvAeNhM+ytuglExv/L4+Fkig3yS0DAJnxbpn Cancel-Lock: sha1:lNVuGS6K1/Hj4pBpTteuWP9v/Q8= sha256:90WVceDj5xPS4hybyr5fxVgh6jhkv8lhK21ggR61cvI= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 3708 ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted: >Embracing these traits leads to more maintainable, efficient, and >understandable code. Instead of trying to force C to act like C++, >it's generally better to leverage C's strengths and use techniques >that are well-suited to the language's design and philosophy. If you absolutely need a library, you (Mark) should totally check out the Python library! It's been around forever, super reliable, and constantly getting better. Here's a quick example program: #include <Python.h> int main() { // Initialize the Python interpreter Py_Initialize(); // Create a Python list PyObject* pyList = PyList_New(0); if (!pyList) { fprintf(stderr, "Failed to create Python list\n"); Py_Finalize(); return 1; } // Add some integers to the list for (int i = 0; i < 5; i++) { PyObject* pyInt = PyLong_FromLong(i); if (!pyInt) { fprintf(stderr, "Failed to create Python integer\n"); Py_DECREF(pyList); Py_Finalize(); return 1; } // PyList_Append increments the reference count of pyInt if (PyList_Append(pyList, pyInt) < 0) { fprintf(stderr, "Failed to append to list\n"); Py_DECREF(pyInt); Py_DECREF(pyList); Py_Finalize(); return 1; } // Decrease reference count of pyInt, as it's now stored in the list Py_DECREF(pyInt); } // Print the list size printf("List size: %zd\n", PyList_Size(pyList)); // Read and print the integers from the list for (int i = 0; i < PyList_Size(pyList); i++) { PyObject* item = PyList_GetItem(pyList, i); // Borrowed reference if (PyLong_Check(item)) { long value = PyLong_AsLong(item); printf("List item %d: %ld\n", i, value); } else { fprintf(stderr, "List item %d is not an integer\n", i); } } // Clean up Py_DECREF(pyList); Py_Finalize(); return 0; } , output: List size: 5 List item 0: 0 List item 1: 1 List item 2: 2 List item 3: 3 List item 4: 4 .