在Python中通过引用和地址调用 [英] Call by reference and address in Python

查看:405
本文介绍了在Python中通过引用和地址调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Python是否支持按引用调用和函数或方法中的地址调用,而不使用C / C ++模块?


谢谢advacne

PSB

解决方案





Python是否支持按引用调用和函数或方法中的地址调用,而不使用C / C ++模块?


感谢advacne

PSB



几乎python中的所有内容都是通过引用传递的。如果你:

展开 | 选择 | Wrap | 行号



但是你不能得到你可以在C中使用aList的地址.python的美妙之处在于我们从不担心变量的位置或者它的生命周期。这一切都得到了照顾。



美女,或者HACKER''S NIGHTMARE?!


我会让你去思考。



美女,或者HACKER''s NIGHTMARE?!


我会留下你去思考。



这是美,因为这意味着通过编写有效的python代码获得以下任何内容的可能性要小得多:


1)内存泄漏(不再需要内存时不释放内存 - 这不包括C / C ++模块的内存泄漏)

2)双重释放(释放已经免费的内存块)

3)悬空指针(指向释放内存的指针)

4)孤立内存(分配内存,但没有以此为主题的指针)

5)错位指针(指向内存错误的指针)


,可能还有更多。


如果一个程序*需要*来直接访问内存和指针,那就是python的错误问题域。我能想到的唯一的内存管理问题是python展示(然后只是通过糟糕的设计)是循环引用,其中A指的是引用A的B.无需小心,删除(从而回收内存)A或B是可能有问题 - 你必须记住在销毁A之前打破对B的引用。


这一切都归结为为工作选择合适的工具。


:-D cybervegan


Hi,

Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?.

Thanks in advacne
PSB

解决方案

Hi,

Is Python supports Call by reference and Call by address in function or methods,without using C/C++ modules?.

Thanks in advacne
PSB

Almost everything in python is pass by reference. If you:

Expand|Select|Wrap|Line Numbers


You can not, however, get the address of aList as you can in C. The beauty of python is that we never worry about where a variable is or what it''s lifespan is. It''s all taken care of.

Beauty, or HACKER''S NIGHTMARE?!

I''ll leave you to ponder.


Beauty, or HACKER''S NIGHTMARE?!

I''ll leave you to ponder.

It''s a beauty, because it means that it''s far less likely that you get any of the following by writing valid python code:

1) memory leaks (where memory is not deallocated when no longer needed -this doesn''t include memory leaks from C/C++ modules)
2) double-frees (freeing an already free''d memory block)
3) dangling pointers (pointers that point to deallocated memory)
4) orphaned memory (memory allocated, but with no pointers amed at it)
5) mangled pointers (pointers that point to the wrong bit of memory)

and probably some more too.

If a program *needs* to access memory and pointers directly, it''s the wrong problem domain for python. The only memory management problem I can think of that python exhibits (and then only by bad design) is cyclic references, where A refers to B which refers to A. Without care, deleting (and thus reclaiming the memory of) A or B is potentially problematic - you have to remember to break the reference to A from B before destroying A.

It all comes down to choosing the right tool for the job.

:-D cybervegan


这篇关于在Python中通过引用和地址调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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