由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - return value of a python function...
相关主题
请教一个python的概念问题怎么学习python
array如何get set?问个Python的问题
Python Q: function pass in struct pointer, come back with data filled请教python
which func will be called?a python question
oop还是跟fp是对立的本版被骂的最多的三个语言
Scala,F#或haskell怎么用DI?TIOBE Index for February 2016
How to check if an object is null in python?请教一个python OOP 实现的问题
python, pickle几年前一个科学预言python的崛起
相关话题的讨论汇总
话题: function话题: return话题: python话题: object话题: mylargeobj
进入Programming版参与讨论
1 (共1页)
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?

1 (共1页)
进入Programming版参与讨论
相关主题
几年前一个科学预言python的崛起oop还是跟fp是对立的
inline functions in C++Scala,F#或haskell怎么用DI?
[合集] Returning heavy weight object in C++ functionHow to check if an object is null in python?
function object VS functionpython, pickle
请教一个python的概念问题怎么学习python
array如何get set?问个Python的问题
Python Q: function pass in struct pointer, come back with data filled请教python
which func will be called?a python question
相关话题的讨论汇总
话题: function话题: return话题: python话题: object话题: mylargeobj