什么时候它是指针(又名参考) - 什么时候是副本? [英] When is it a pointer (aka reference) - when is it a copy?
问题描述
嗨列表,
只是为了确保我理解这一点。
因为没有指针输入Python,我想知道我是怎么做的。
例如,如果我这样做:
... some_huge_list是一个巨大的列表...
some_huge_list [0] = 1
aref = some_huge_list
aref [0] = 0
打印some_huge_list [0]
我们知道答案将是0.在这种情况下,aref真的是
reference。
但是如果右边是一个简单的变量(比如说int)怎么办?可以
我参考"不知怎的?我应该假设:
aref = _any_type_other_than_simple_one
是参考,而不是副本?
谢谢,
Hi list,
Just to make sure I understand this.
Since there is no "pointer" type in Python, I like to know how I do
that.
For instance, if I do:
...some_huge_list is a huge list...
some_huge_list[0]=1
aref = some_huge_list
aref[0]=0
print some_huge_list[0]
we know that the answere will be 0. In this case, aref is really a
reference.
But what if the right hand side is a simple variable (say an int)? Can
I "reference" it somehow? Should I assume that:
aref = _any_type_other_than_simple_one
be a reference, and not a copy?
Thanks,
推荐答案
JohnHenryírta:
John Henry írta:
嗨列表,
只是为了确保我理解这一点。
因为没有指针输入Python,我想知道我是怎么做的。
例如,如果我这样做:
... some_huge_list是一个巨大的列表...
some_huge_list [0] = 1
aref = some_huge_list
aref [0] = 0
打印some_huge_list [0]
我们知道答案将是0.在这种情况下,aref真的是
reference。
但是如果右边是一个简单的变量(比如说int)怎么办?可以
我参考"不知怎的?我应该假设:
aref = _any_type_other_than_simple_one
是参考,而不是副本?
Hi list,
Just to make sure I understand this.
Since there is no "pointer" type in Python, I like to know how I do
that.
For instance, if I do:
...some_huge_list is a huge list...
some_huge_list[0]=1
aref = some_huge_list
aref[0]=0
print some_huge_list[0]
we know that the answere will be 0. In this case, aref is really a
reference.
But what if the right hand side is a simple variable (say an int)? Can
I "reference" it somehow? Should I assume that:
aref = _any_type_other_than_simple_one
be a reference, and not a copy?
简短的回答是你需要将不可变值保存在
可变对象中。
答案很长:
a。)你可以像任何对象一样引用不可变对象,但是你不能更改不可变对象。例如,当你做的时候
a = 1
a + = 2
然后你重新绑定变量' 'a''到另一个对象(即,
2 int对象。)
b。)但是,您可以保留对当前内容的引用不可变对象
在一个可变对象中。在许多情况下,可变对象将是一个
命名空间字典。
模块是一个非常简单的例子:
a = 1
def f1():
全球a
a + = 1
def f2():
全球a
a + = 10
f1()
f2()
打印#打印12
请注意,a + = 10实际上会将变量重新绑定到另一个
对象。
在其他情况下,您将使用对象或类:
A类(对象):
a = 5
def inc_id(obj):
obj。 id + = 1
a = A()#a.id是5这里
a.id = 4#使a.id引用4
inc_id(a)#使a.id再次引用5
Best,
Laszlo
The short answer is that you need to keep the immutable value inside a
mutable object.
The long answer:
a.) You can reference the immutable object just like any object, but you
cannot change the immutable object. For instance, when you do
a = 1
a += 2
then you are rebinding the variable ''a'' to a different object (namely,
the 2 int object.)
b.) You can however, keep a reference to your current immutable object
inside a mutable object. In many cases, the mutable object will be a
namespace dictionary.
A module is a very simple example:
a = 1
def f1():
global a
a += 1
def f2():
global a
a += 10
f1()
f2()
print a # prints 12
Notice that the "a+=10" will actually rebind the variable to a different
object.
In other cases, you will be using an object or a class:
class A(object):
a = 5
def inc_id(obj):
obj.id += 1
a = A() # a.id is 5 here
a.id = 4 # makes a.id a reference to 4
inc_id(a) # makes a.id a reference to 5 again
Best,
Laszlo
John Henry写道:
John Henry wrote:
嗨列表,
只是为了确保我理解这一点。
因为没有指针输入Python,我想知道我是怎么做的。
例如,如果我这样做:
... some_huge_list是一个巨大的列表...
some_huge_list [0] = 1
aref = some_huge_list
aref [0] = 0
打印some_huge_list [0]
我们知道答案将是0.在这种情况下,aref真的是
reference。
但是如果右边是一个简单的变量(比如说int)怎么办?可以
我参考"不知怎的?我应该假设:
aref = _any_type_other_than_simple_one
是参考,而不是副本?
Hi list,
Just to make sure I understand this.
Since there is no "pointer" type in Python, I like to know how I do
that.
For instance, if I do:
...some_huge_list is a huge list...
some_huge_list[0]=1
aref = some_huge_list
aref[0]=0
print some_huge_list[0]
we know that the answere will be 0. In this case, aref is really a
reference.
But what if the right hand side is a simple variable (say an int)? Can
I "reference" it somehow? Should I assume that:
aref = _any_type_other_than_simple_one
be a reference, and not a copy?
是的。属性始终是对象引用。赋值实际上是特定对象与某个命名空间中的名称的绑定,(r到序列或其他容器对象的
元素。
这适用于任何* RHS表达式的类型:表达式
被评估以产生一个对象,并且存储对象的引用
在名称或容器元素中。
问候
Steve
-
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC / Ltd http://www.holdenweb.com
Skype:holdenweb http://holdenweb.blogspot.com
最近的Ramblings http://del.icio.us/steve.holden
Yes. Attributes are always object references. The assignment is actually
the binding of a specific object to a name in some namespace, (r to an
element of a sequence or other container object.
This applies *whatever* the type of the RHS experession: the expression
is evaluated to yield an object, and a reference to the object is stored
in the name or container element.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
感谢您对Laszl的回复o和史蒂夫。
好的,我明白你在说什么。
但是如果我需要制作一个指针怎么办? ;一个简单的变量。
例如,在C:
int i = 1
int * j =& i
* j = 2
打印我
你得到2张打印。 />
在Python中,
i = 1
j = i
j = 2 >
打印我
你打印1张。
所以,如果我理解正确的话,我必须参考更精彩的代表性。喜欢:
i = [1,]
j = i
j [0] = 2
打印我
以便打印2张。
正确吗?
Steve Holden写道:
Thanks for the reply, both to Laszlo and Steve.
Okay, I understand what you''re saying.
But what if I need to make a "pointer" to a simple variable.
For instance, in C:
int i=1
int *j=&i
*j = 2
print i
and you get 2 printed.
In Python,
i=1
j=i
j=2
print i
and you get 1 printed.
So, if I understand you correctly, I must make the reference to a more
elaborate representation. Like:
i=[1,]
j=i
j[0]=2
print i
in order to get 2 printed.
Correct?
Steve Holden wrote:
John Henry写道:
John Henry wrote:
嗨列表,
只是为了确保我理解这一点。
因为没有指针输入Python,我想知道我是怎么做的。
例如,如果我这样做:
... some_huge_list是一个巨大的列表...
some_huge_list [0] = 1
aref = some_huge_list
aref [0] = 0
打印some_huge_list [0]
我们知道答案将是0.在这种情况下,aref真的是
reference。
但是如果右边是一个简单的变量(比如说int)怎么办?可以
我参考"不知怎的?我应该假设:
aref = _any_type_other_than_simple_one
是一个参考,而不是副本?
Hi list,
Just to make sure I understand this.
Since there is no "pointer" type in Python, I like to know how I do
that.
For instance, if I do:
...some_huge_list is a huge list...
some_huge_list[0]=1
aref = some_huge_list
aref[0]=0
print some_huge_list[0]
we know that the answere will be 0. In this case, aref is really a
reference.
But what if the right hand side is a simple variable (say an int)? Can
I "reference" it somehow? Should I assume that:
aref = _any_type_other_than_simple_one
be a reference, and not a copy?
是的。属性始终是对象引用。赋值实际上是特定对象与某个命名空间中的名称的绑定,(r到序列或其他容器对象的
元素。
这适用于任何* RHS表达式的类型:表达式
被评估以产生一个对象,并且存储对象的引用
在名称或容器元素中。
问候
Steve
-
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC / Ltd http://www.holdenweb.com
Skype:holdenweb http://holdenweb.blogspot.com
最近的Ramblings http://del.icio.us/steve.holden
这篇关于什么时候它是指针(又名参考) - 什么时候是副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!