Python中的指针? [英] Pointers in Python?
问题描述
我知道Python没有指针,但是有办法代替2
I know Python doesn't have pointers, but is there a way to have this yield 2
instead
>>> a = 1
>>> b = a # modify this line somehow so that b "points to" a
>>> a = 2
>>> b
1
?
这里是一个示例:我希望form.data['field']
和form.field.value
始终具有相同的值.并非完全必要,但我认为这会很好.
Here's an example: I want form.data['field']
and form.field.value
to always have the same value. It's not completely necessary, but I think it would be nice.
例如,在PHP中,我可以这样做:
In PHP, for example, I can do this:
<?php
class Form {
public $data = [];
public $fields;
function __construct($fields) {
$this->fields = $fields;
foreach($this->fields as &$field) {
$this->data[$field['id']] = &$field['value'];
}
}
}
$f = new Form([
[
'id' => 'fname',
'value' => 'George'
],
[
'id' => 'lname',
'value' => 'Lucas'
]
]);
echo $f->data['fname'], $f->fields[0]['value']; # George George
$f->data['fname'] = 'Ralph';
echo $f->data['fname'], $f->fields[0]['value']; # Ralph Ralph
输出:
GeorgeGeorgeRalphRalph
或者在C ++中这样(我认为这是正确的,但我的C ++生锈了):
Or like this in C++ (I think this is right, but my C++ is rusty):
#include <iostream>
using namespace std;
int main() {
int* a;
int* b = a;
*a = 1;
cout << *a << endl << *b << endl; # 1 1
return 0;
}
推荐答案
我想要
form.data['field']
并且form.field.value
始终具有 相同的值
I want
form.data['field']
andform.field.value
to always have the same value
这是可行的,因为它涉及修饰的名称和索引-即,完全与您要查询的 barenames a
和b
不同的构造关于,因为您的要求是完全不可能的.为什么要请求与您实际想要的(可能)事物完全不同的和?!
This is feasible, because it involves decorated names and indexing -- i.e., completely different constructs from the barenames a
and b
that you're asking about, and for with your request is utterly impossible. Why ask for something impossible and totally different from the (possible) thing you actually want?!
也许您不知道裸名和修饰名有多么大的不同.当您引用裸名a
时,您恰恰获得了对象a
最终绑定到该范围的对象(如果未绑定到此范围,则为例外),这是一个深刻而基本的内容Python的一个方面,它不可能被颠覆.当您指代装饰的名称x.y
时,您是在问一个对象(对象x
所指)请提供"y
属性"-并对此做出响应请求,该对象可以执行完全任意的计算(并且索引非常相似:它还允许响应地执行任意计算).
Maybe you don't realize how drastically different barenames and decorated names are. When you refer to a barename a
, you're getting exactly the object a
was last bound to in this scope (or an exception if it wasn't bound in this scope) -- this is such a deep and fundamental aspect of Python that it can't possibly be subverted. When you refer to a decorated name x.y
, you're asking an object (the object x
refers to) to please supply "the y
attribute" -- and in response to that request, the object can perform totally arbitrary computations (and indexing is quite similar: it also allows arbitrary computations to be performed in response).
现在,您的实际需求"示例很神秘,因为在每种情况下都涉及两个级别的索引编制或属性获取,因此可以通过多种方式引入您渴望的精妙之处.例如,除了value
之外,form.field
还应该具有哪些其他属性?如果没有进一步的.value
计算,可能性将包括:
Now, your "actual desiderata" example is mysterious because in each case two levels of indexing or attribute-getting are involved, so the subtlety you crave could be introduced in many ways. What other attributes is form.field
suppose to have, for example, besides value
? Without that further .value
computations, possibilities would include:
class Form(object):
...
def __getattr__(self, name):
return self.data[name]
和
class Form(object):
...
@property
def data(self):
return self.__dict__
.value
的存在建议选择第一种形式,再加上一种无用的包装器:
The presence of .value
suggests picking the first form, plus a kind-of-useless wrapper:
class KouWrap(object):
def __init__(self, value):
self.value = value
class Form(object):
...
def __getattr__(self, name):
return KouWrap(self.data[name])
如果还应该在form.data
中设置 assignments 这样的form.field.value = 23
条目,那么包装器的确必须变得更加复杂,并且不是所有没用的东西:
If assignments such form.field.value = 23
is also supposed to set the entry in form.data
, then the wrapper must become more complex indeed, and not all that useless:
class MciWrap(object):
def __init__(self, data, k):
self._data = data
self._k = k
@property
def value(self):
return self._data[self._k]
@value.setter
def value(self, v)
self._data[self._k] = v
class Form(object):
...
def __getattr__(self, name):
return MciWrap(self.data, name)
后一个示例在Python中与您似乎想要的指针"意义大致相近-但至关重要的是要了解这样的细微差别只能在 indexing <下使用/em>和/或修饰的名称,从不,使用您最初要求的裸名!
The latter example is roughly as close as it gets, in Python, to the sense of "a pointer" as you seem to want -- but it's crucial to understand that such subtleties can ever only work with indexing and/or decorated names, never with barenames as you originally asked!
这篇关于Python中的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!