D*****r 发帖数: 6791 | 1 Does a python function return a reference to a large mutable object?
I can't read chinese on this machine...
My question is:
If I use a function to create an object (for example a browser object from
mechanize module, or a dictionary from reading a csv file) and return it,
does it only return a reference?
I am doing work on these large objects and the function is getting too long.
I want to use seperate functions to do work. But I certainly don't want the
large object to be duplicated or disappear.
Can someone explain how can I do it? Or is there a better way? |
d****n 发帖数: 1637 | 2 My simple ideas.
1. use global in your function
def func( ):
global myLargeObj
modify myLargeObj
2. pass obj by parameter but all your change is on a mutable obj.
def func(myLargeObj):
modify myLargeObj
"""
Note: dont use return statement here.
"""
E.g.
>>> d={}
>>> d[1]=123
>>> def func(dd):
... del dd[1]
...
>>> func(d)
>>> d
{}
>>> |
X****r 发帖数: 3557 | 3 Yes.
All objects are passed by reference, in or out.
long.
the
【在 D*****r 的大作中提到】 : Does a python function return a reference to a large mutable object? : I can't read chinese on this machine... : My question is: : If I use a function to create an object (for example a browser object from : mechanize module, or a dictionary from reading a csv file) and return it, : does it only return a reference? : I am doing work on these large objects and the function is getting too long. : I want to use seperate functions to do work. But I certainly don't want the : large object to be duplicated or disappear. : Can someone explain how can I do it? Or is there a better way?
|
l********a 发帖数: 1154 | 4 一直被java鼓吹,却并未真正实现的"一切皆对象",在python这里完全实现了
所有数据类型都是对象,python的对象使用的是引用计数,
当引用计数为0或引用refer到不可用对象时,gc模块会定期收集垃圾
举个例子
a = 3.0 # 3.0对象,a引用它,3.0的引用为1
b = 2 # 3.0引用计数+1=2
del a # 3.0引用计数-1=1
del b # 3.0引用计数-1=0,下次gc模块动作时会被清除 |
g*****g 发帖数: 34805 | 5 得,java是为了效率才那么做。一切皆对象从来不是java鼓吹的东西,
老古董smalltalk都已经实现了。
【在 l********a 的大作中提到】 : 一直被java鼓吹,却并未真正实现的"一切皆对象",在python这里完全实现了 : 所有数据类型都是对象,python的对象使用的是引用计数, : 当引用计数为0或引用refer到不可用对象时,gc模块会定期收集垃圾 : 举个例子 : a = 3.0 # 3.0对象,a引用它,3.0的引用为1 : b = 2 # 3.0引用计数+1=2 : del a # 3.0引用计数-1=1 : del b # 3.0引用计数-1=0,下次gc模块动作时会被清除
|
m*****e 发帖数: 4193 | 6 Python is garbage collected AND reference counted. Objects are passed by
references. |
l********a 发帖数: 1154 | 7 汗..无意挑起语言之争,各有利弊,无非是个工具而已.
【在 g*****g 的大作中提到】 : 得,java是为了效率才那么做。一切皆对象从来不是java鼓吹的东西, : 老古董smalltalk都已经实现了。
|
D*****r 发帖数: 6791 | 8 多谢各位!对象还好办,
现在我是要用一个大字符串,来调一条网络服务来干活。
这个大字符串是不是没法用引用传了?只能复制一遍传到函数里?
【在 m*****e 的大作中提到】 : Python is garbage collected AND reference counted. Objects are passed by : references.
|
X****r 发帖数: 3557 | 9 字符串也是对象,自然传的也是引用。注意字符串的值是不能改变的。
【在 D*****r 的大作中提到】 : 多谢各位!对象还好办, : 现在我是要用一个大字符串,来调一条网络服务来干活。 : 这个大字符串是不是没法用引用传了?只能复制一遍传到函数里?
|
r****t 发帖数: 10904 | 10 一个 c++ 的程序员看到 python 时候的悲剧。
long.
the
【在 D*****r 的大作中提到】 : Does a python function return a reference to a large mutable object? : I can't read chinese on this machine... : My question is: : If I use a function to create an object (for example a browser object from : mechanize module, or a dictionary from reading a csv file) and return it, : does it only return a reference? : I am doing work on these large objects and the function is getting too long. : I want to use seperate functions to do work. But I certainly don't want the : large object to be duplicated or disappear. : Can someone explain how can I do it? Or is there a better way?
|