473,836 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling python functions from C

I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

R.

May 10 '06 #1
5 2083
ro************* ****@gmail.com wrote:
I want to call add from C. Could anybody please help me?


Look at the "Pure Embedding" section of the python documentation. It
contains an example program that does exactly what you want. Here is a
link to the page:

http://www.python.org/doc/ext/pure-embedding.html

-Farshid
May 10 '06 #2
On 9 May 2006 10:05:48 -0700, ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance. This is also known as "callback".

Check out
http://www.google.com/search?client=...utf-8&oe=utf-8
or
http://www.python.org/doc/current/ext/ext.html

-RM
R.

May 10 '06 #3
Thanks Farshid,

Thats helps me alot !

R.
Farshid Lashkari wrote:
ro************* ****@gmail.com wrote:
I want to call add from C. Could anybody please help me?


Look at the "Pure Embedding" section of the python documentation. It
contains an example program that does exactly what you want. Here is a
link to the page:

http://www.python.org/doc/ext/pure-embedding.html

-Farshid


May 10 '06 #4
Thanks Ronny,

R.

Ronny Mandal wrote:
On 9 May 2006 10:05:48 -0700, ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

This is also known as "callback".

Check out
http://www.google.com/search?client=...utf-8&oe=utf-8
or
http://www.python.org/doc/current/ext/ext.html

-RM

R.


May 10 '06 #5
ro************* ****@gmail.com wrote:
I am a newbie to Python. I want to call python functions from C. I
looked for examples but I couldn't get any simple one. Lets say my
python code is :
def add(a,b)
return (a+b)

I want to call add from C. Could anybody please help me? Thanks in
advance.

R.


You might look at elmer <http://elmer.sourcefor ge.net/>

Quote:
What is elmer?
Elmer is a tool which embeds a Python module into a C, C++, or Tcl
application. The embedded Python module is used just as if it was
written in the same language as the application itself, without
requiring any knowledge of the Python/C API.

Elmer generates "glue" code that embeds an interpreter for one type of
language into an extension for another. Elmer allows function calls and
various data types, both native and custom, to pass transparently
between the two languages.
--
Nigel Rowe
A pox upon the spammers that make me write my address like..
rho (snail) swiftdsl (stop) com (stop) au
May 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2056
by: Kyler Laird | last post by:
I need to submit C/C++ code for a class. (It's not a programming class. The choice of language is inertial. I think that it mostly serves to distract students from the course subject.) I'm fairly fluent with C but it hurts to think about writing in C when Python is *so* much more appropriate for these operations. I'd like to keep my sanity and satisfy the course requirements by programming in Python and converting the code to C. It...
0
2326
by: Natsu Mizutani | last post by:
Hello, I'm trying to wrap a c++ library using MPI inside with boost.python (or SWIG). I managed to find that calling `MPI::Init()` embeded in a c++ funtion would not work. So, I decided to use `pyMPI`. To avoid overhead of pickling and unpickling while calling `mpi.send` in Python, I'd rather call c++ functions `MPI::Isend()` etc. embeded in a c++ function.
18
5309
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
1
1740
by: delrocco | last post by:
Hello, I really appreciate anyone who has time to read this and help, Thanks up front. I'm very new to python, having picked it up for the first time a week ago, but I feel I'm very close to getting this working. Here's what I'm trying to do: - call python scripts from game code - have those called scripts call functions that are back in game code
6
1553
by: Yevgeniy (Eugene) Medynskiy | last post by:
Hi all, This is probably a very newbie question, but after searching google and docs @ python.org I can't find an answer, so maybe someone would be able to help? I'd like to call command-line functions from my python script (like you would in perl using backticks)... Is there a way of doing this? And if so, how does the environment get treated (I have some variables in my env that the programs I'd be calling need to see).
1
1928
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss something? Cya, Gerald -----Ursprüngliche Nachricht----- Von: python-list-bounces+gerald.klix=klix.ch@python.org Im Auftrag von
3
5471
by: David Bear | last post by:
I have a hash function written by another organization that I need to use. It is implemented in perl. I've been attempting to decode what they are doing in their hash function and it is taking way too long. I've identified two functions in a perl module that I would like to 'call' from a python program. I found the following: http://www.annocpan.org/~GAAS/pyperl-1.0/perlmodule.pod and wondered if anyone had any comments. This thing...
4
4803
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread completes it invokes a method from the calling thread. Sort event driven concept with threads. Thanks. Ed Gomez
47
4991
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
16
3792
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any warnings etc. I've done this in Debug and Release mode without any problems.
0
9812
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10534
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10579
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4444
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.