Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43844 > unrolled thread

Re: equivalent to C pointer

Started byabdelkader belahcene <abelahcene@gmail.com>
First post2013-04-18 18:50 +0100
Last post2013-04-18 13:48 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: equivalent to C pointer abdelkader belahcene <abelahcene@gmail.com> - 2013-04-18 18:50 +0100
    Re: equivalent to C pointer Neil Cerutti <neilc@norwich.edu> - 2013-04-18 18:07 +0000
      Re: equivalent to C pointer Tim Chase <python.list@tim.thechases.com> - 2013-04-18 13:48 -0500

#43844 — Re: equivalent to C pointer

Fromabdelkader belahcene <abelahcene@gmail.com>
Date2013-04-18 18:50 +0100
SubjectRe: equivalent to C pointer
Message-ID<mailman.783.1366307409.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Thanks for answer,
but with C  we can compile the trapeze function and put it in librairy,
If we try to save the trapeze alone in  package to import it later,  I
think, I am not sure
it will be refused because F1 and sin are not define !!!     this is the
power of the C pointers !!!
the link is dynamic

thanks



On Thu, Apr 18, 2013 at 6:17 PM, Karim <kliateni@gmail.com> wrote:

>
> Hello,
>
> There is no such notion in python.
> But the closest are iterators and generator functions.
>
> Cheers
> Karim
>
>
> On 18/04/2013 19:06, abdelkader belahcene wrote:
>
>     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
>
>
>
>

[toc] | [next] | [standalone]


#43847

FromNeil Cerutti <neilc@norwich.edu>
Date2013-04-18 18:07 +0000
Message-ID<atar34Fmv3jU2@mid.individual.net>
In reply to#43844
On 2013-04-18, abdelkader belahcene <abelahcene@gmail.com> wrote:
> Thanks for answer,
> but with C  we can compile the trapeze function and put it in
> librairy, If we try to save the trapeze alone in  package to
> import it later,  I think, I am not sure it will be refused
> because F1 and sin are not define !!!     this is the power of
> the C pointers !!! the link is dynamic

There's no linking stage in Python. Everything you use must be
defined before you use it.

In Python you can put trapeze in a library, and it will be able
to accept any old function under the sun when you call it, as
long as that function is defined.

in trapeze.py:

  def trapeze(func, left, right, step):
      return sum((func(x) + func(x + step)) * step * 0.5
                 for x in range(left, right, step))
               
in file1.py:

  import trapeze
  
  def square(x):
      return x*x
  
  print(trapeze.trapeze(square, 0, 3, 2.5))


if file2.py:

  import trapeze
  import math
  
  print(trapeze.trapeze(math.sin, 1.3, 2.5, 1.0))

The functions square and sin are both defined before you pass
them to trapeze, so all is well. Trapeze doesn't know or care about
the signature of those functions until it actually tries to call
them. At that time, if either one isn't defined properly Python
will raise an exception.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#43852

FromTim Chase <python.list@tim.thechases.com>
Date2013-04-18 13:48 -0500
Message-ID<mailman.788.1366310836.3114.python-list@python.org>
In reply to#43847
On 2013-04-18 18:07, Neil Cerutti wrote:
> There's no linking stage in Python. Everything you use must be
> defined before you use it.

"must be defined", only if you don't want an error.  But in python,
it isn't even REQUIRED that it be defined:

  some_undefined_function("args go here")

will bomb out your program, but Python graciously allows you to do so:

  >>> try:
  ...     hello(42)
  ... except NameError:
  ...     print "You had me at hello"
  ... 
  You had me at hello

-tkc


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web