在三分球蟒 [英] pointers in python

查看:122
本文介绍了在三分球蟒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是物理系学生,大多以Python进行编程和Matlab,现在进入C到提高我的模拟性能。

I'm a physics student, mostly programming in Python and Matlab, now getting into C to boost the performance of my simulations.

简单的问题:有没有指针在​​Python?有没有在Matlab指针?如果是的话,我可以用它们与类似的放心在C?

Simple question: are there pointers in Python? Are there pointers in Matlab? If yes, can I use them with similar "ease" as in C?

我知道,在Python和Matlab的高匀染性的背后,有很多工作的指针。现在的问题是:我可以这样做这款C code明确在Python或Matlab:

I know that behind the high-levelness in Python and Matlab, there are lots of pointers at work. The question is: can I do something like this C code explicitly in Python or Matlab:

int *p;
int someValue = 1;
p = &someValue;
doSomethingForMe = callSomeFunction(p);

我知道可能有一个在这样做呢(多数情况下)没有任何意义。我只是想了解。

I know there's probably no point in doing that anyway (for most applications). I just want to learn.

推荐答案

您会经常听到的 Python有名字,而不是传统意义上的变量。它也没有指针,因为在较低的水平与C时,像这样的概念才有意义(因为这个原因,你可以得到指针,如果你在Python中使用C兼容库,如 ctypes的。但是,这样的指针即只允许使用C互操作性。)

You will commonly hear that Python has names, not "variables" in the traditional sense. It also doesn't have "pointers" since such a concept is only meaningful when working at a low level, like with C. (For this reason, you can get "pointers" if you use a C compatibility library in Python, like ctypes. But, such pointers are meant only for interoperability with C.)

你们大多数人会用C使用指针的功能是在Python一些其他形式的present。例如:

Most of the functionality you would use a pointer for in C is present in some other form in Python. For example:


  • 从一个函数返回多个值。在C语言中,你会做 INT美孚(INT *,为int *)返回两个整数(也许是错误code)。在Python,只需使用元组拆包: X,Y = foo的()收益的x,y

  • 传递大的对象。因为一切都是通过通过引用无论如何,你可以安全地传递到一个巨大的对象的引用,而无需它被复制。

  • 修改输入参数。大部分的这是可能通过选择适当的可变输入。例如,您可以传递一个列表到函数和函数中修改列表。在Python,由于引用是相同的,输入列表将出现修改

  • Returning multiple values from a function. In C, you'd do int foo(int *, int *) to return two ints (and maybe an error code). In Python, just use tuple unpacking: x, y = foo() with return x, y in foo.
  • Passing in big objects. Since everything is pass-by-reference anyway, you can safely pass a reference to a huge object without having it get copied.
  • Modifying input parameters. Most of this is possible by choosing an appropriate mutable input. For example, you can pass a list into a function and modify the list within the function. In Python, since the references are the same, the input list will appear to be modified:

def change_list(l):
    l[0] = 3
my_list = [1,2,3]
change_list(my_list)
print(my_list) # prints [3, 2, 3]


MATLAB具有变量的不同的概念Python做。在MATLAB中,变量是用某些维度的矩阵。写 X = Y 有效副本 X (设置 X 适当s尺寸)。 MATLAB内部做优化,以避免实际除非必要复制数据,但对于程序员会看起来好像 X 是独立的矩阵。

MATLAB has a different concept of variable than Python does. In MATLAB, a variable is a matrix with some dimensions. Writing x = y effectively copies y into x (setting xs dimensions appropriately). MATLAB internally does optimizations to avoid actually copying data unless necessary, but for the programmer it will seem as if x and y are separate matrices.

MATLAB也没有指针,除非你正在处理一个编译MEX扩展(这是写在其他语言如C)。如Python,你有身边路过事,而不需要指针(多输入和输出参数,例如)的机制。

MATLAB also doesn't have pointers unless you are dealing with a compiled MEX extension (which is written in some other language like C). Like Python, you have mechanisms for passing things around without needing pointers (multiple input and output arguments, for example).

这篇关于在三分球蟒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆