LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2013, 12:10 PM   #1
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Rep: Reputation: 31
is there pointers in python


Hi everybody,

I am new to python and I am discovering it.
I know C well,
and want to know if python knows how to manage Pointers
like pointer to function here is a C example how to write it in python
Intergration with trapeze method

When we write Trapeze ( at the compilation level) we don't know which functions
Fonc to handle. Here for example we use sin and a user defined F1
The program is attached too

#include <stdio.h>
#include <math.h>

double F1 (double x){
return x*x;
}
double Trapeze(double Fonc(double ),
double left, double right, double step){
double X1, X0, Y0, Y1, Z = 0;
for(X0=left; X0 < right ; X0 = X0 + step) {
X1 = X0 + step;
Y1 = Fonc(X1); Y0 = Fonc(X0);
Z += (Y1 + Y0) * step * 0.5;
}
return Z;
}
int main(){
double y;
y=Trapeze(sin, -2.5, 3.2, 0.1);
printf("\n\tValue for sin is : \t %8.3lf ", y);
y=Trapeze(F1, 0, 3, 0.1);
printf("\n\tValue for F1 is : \t %8.3lf ", y);
return 0;
}
/**
Value for sin is : 0.197
Value for F1 is : 9.005
*/
thanks a lot
 
Old 04-18-2013, 12:19 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

basically everything in python is a reference (except for simple types like ints etc), so, yes you can do something like "pointer to a function". Eg try the following:

Code:
def x(f, a):
  f(a)

def y(s):
  print s

x( y, "Hello" )
Evo2.

PS. Please use [code] tags... it makes reading code, so much less painful.
 
Old 04-18-2013, 12:54 PM   #3
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Original Poster
Rep: Reputation: 31
thanks

but when we use the x fonction y is define too, we know it
this is not my case when I wrote trapeze I didn't care about the argument function!!!

My C program may be split in 2 parts: the library with trapeze and the test program to check it
with main , F1 and sin

Try to write same thing in python
cheers
 
Old 04-18-2013, 01:06 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

sorry I don't see what the problem is. If you really can't work it out from the example I gave, please repost your c code in the simplest form, properly indeted in [code] tags, and I'll write the equivalent python. When I say simplest, I mean it just has to demonstrate the functionality you are looking for, not actually do anything useful.

Evo2.
 
Old 04-18-2013, 01:41 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Ok, I'm on my lunch break so I decided to do it...
Code:
import math

def F1(x):
    return x*x

def Trapeze( f, left, right, step ):
    x1=x0=y0=y1=z=0.0
    for i in range( int((right-left)/step) ):
        x0=left+i*step
        x1=x0+step
        y1=f(x1)
        y0=f(x0)
        z += (y1+y0)*step*0.5
    return z

if __name__ == '__main__':
    print 'Value for sin is %f' % Trapeze( math.sin, -2.5, 3.2, 0.1 )
    print 'Value for F1  is %f' % Trapeze( F1, 0, 3, 0.1)
It runs and outputs the following:
Code:
Value for sin is 0.196987
Value for F1  is 9.005000
Note that I would not normally write code like this: I wrote it this way to make it as close as possible to the sample c code.


Evo2.

Last edited by evo2; 04-18-2013 at 01:51 PM.
 
Old 04-20-2013, 08:14 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In high-level languages like Python, "a reference" is a very similar thing to "a pointer" ... except that it is smart.
  • The language system knows what sort of thing any reference is referring to.
  • The thing being referred to has a "reference count," which keeps it from being destroyed when something refers to it.
  • There is a "garbage collector" that will dispose of objects that are no longer being referenced.
This is, quite frankly, one of the most-important reasons why these language systems are routinely used. C/C++ is used to implement them, but with only a few lines of high-level language code you can (very efficiently) tap into "all sorts of language goodness" (tens of thousands of lines' worth of scrupulously optimized, magical voodoo C/C++ code) that makes your life considerably easier. The overhead of the system is predictable, and relatively inconsequential.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python wins trademark dispute, rival stops calling itself “Python” LXer Syndicated Linux News 0 03-21-2013 07:40 PM
[SOLVED] Freeing pointers to pointers devnull10 Programming 24 07-26-2012 04:58 AM
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 11:05 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration