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


Groups > comp.lang.python > #43847

Re: equivalent to C pointer

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: equivalent to C pointer
Date 2013-04-18 18:07 +0000
Organization Norwich University
Message-ID <atar34Fmv3jU2@mid.individual.net> (permalink)
References <CAF3f0sQ_rsdKwaEOCmFHEeEwpVUaNUo-pOYgoqYA_F-k0CftcA@mail.gmail.com> <51702AB6.30609@gmail.com> <mailman.783.1366307409.3114.python-list@python.org>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web