Python中的指针? [英] Pointers in Python?

查看:84
本文介绍了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

ideone

或者在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'] and form.field.value to always have the same value

这是可行的,因为它涉及修饰的名称和索引-即,完全与您要查询的 barenames ab不同的构造关于,因为您的要求是完全不可能的.为什么要请求与您实际想要的(可能)事物完全不同的?!

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆