简体   繁体   English

将指针作为参数传递给py中的函数

[英]passing pointer as argument to function in py

I have 2 function that use the same variable, like this: 我有2个使用相同变量的函数,如下所示:

some_function():
    #do something with MyDict
    MyDict = dict()
    MyDict['name'] = 'hallo'

main():
    #do something else with MyDict
    MyDict['age'] = 33
    some_function()

I don't want to use global and let say that MyDict is very big and I don't want to pass it to some_function . 我不想使用global ,可以说MyDict非常大,也不想将其传递给some_function what is the best way to do so? 最好的方法是什么? there is a way to pass a pointer to MyDict ? 有没有办法将指针传递给MyDict PS: I'm using py3.2 PS:我正在使用py3.2

In short: when you pass a dict as argument to a function, only the reference is passed, the data is not copied. 简而言之:将dict作为参数传递给函数时,仅传递引用,而不会复制数据。

However, in general the parameters are references but some types are mutables but others aren't which changes the behavior. 但是,一般而言,参数是引用,但某些类型是可变的,而其他类型则不能改变行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM