Python - copy by reference

Is it possible to copy a variable by reference regardless of its int instance or class?

My goal is to have two lists of the same objects, and when they change, the change is displayed in the second.

In other words, I need pointers: /


I just want int, float and other standard types that are usually copied by value, force copy by reference. This will make my code more consistent.

If this is not possible, then the best solution would be a class wrapper.

+5
source share
5 answers

(edited to show an example of dereferencing in the same memory location)

Luper Rouch . .

C-, ( , ..), ctypes. c FFI DLL.

from ctypes import *
containerTYPE = POINTER( c_uint ) * 10 #Array of pointers to UINTs of size 10
containerA = containerTYPE()
containerB = containerTYPE()

for i in range( 10 ):
    val = c_uint( i )
    containerA[ i ] = pointer( val ) 
    containerB[ -1 - i ] = pointer( val ) 

print "A\tB"
for i in range( 10 ):
    print containerA[ i ].contents.value, "\t", containerB[ i ].contents.value

for i in range( 10 ): #affects both arrays
    derefed = containerA[ i ].contents
    derefed.value = i * 2

print
print "A\tB"
for i in range( 10 ):
    print containerA[ i ].contents.value, "\t", containerB[ i ].contents.value

:

A       B
0       9
1       8
2       7
3       6
4       5
5       4
6       3
7       2
8       1
9       0

A       B
0       18
2       16
4       14
6       12
8       10
10      8
12      6
14      4
16      2
18      0
0

:

class MutableWrapper(object):

    def __init__(self, value):
        self.value = value

a = MutableWrapper(10)
b = a
a.value = 20
assert b.value == 20
+8

Python , ( " " ), numpy ). - alist=anotherlist; alist.sort() , ( alist anotherlist) - .

, (, alist=list(anotherlist)) - , , . : ( , , !), ( ).

, - (), , ... , , ( , : , , , - - , , - , ).

, , , ; , , , , - , , , , . . :

>>> alist = list([x] for x in 'ciao')
>>> blist = list(alist)
>>> blist.sort()
>>> alist
[['c'], ['i'], ['a'], ['o']]
>>> blist
[['a'], ['c'], ['i'], ['o']]
>>> blist[-1][0] = 'z'
>>> blist
[['a'], ['c'], ['i'], ['z']]
>>> alist
[['c'], ['i'], ['a'], ['z']]

, , , , , ; -).

+8

, API . , -

import bisect

class DualLists(object):
    def __init__(self, iterable=[]):
        self.insertion_order = list(iterable)
        self.sorted = sorted(self.insertion_order)

    def append(self, item):
        self.insertion_order.append(item)
        bisect.insort(self.sorted, item)

>>> d = DualLists()
>>> d.append(4)
>>> d.append(6)
>>> d.append(1)
>>> d.insertion_order
[4, 6, 1]
>>> d.sorted
[1, 4, 6]

, blist , bisect list. , , , sqlite3.

0

, , , . , .

Based on what you have given so far, I would subclass the built-in list type and keep its alternate version. Override the list methods to work on yourself and the alternative version, where it makes sense. If this does not make sense, for example, in a function sort(), define a second function for the alternate list.

It really makes sense if the variety is unreasonably expensive; otherwise, I would just save one list and sort on demand.

class MyList(list):

    def __init__(self, li):
        super(MyList, self).__init__(li)
        self.altlist = list(li)

    def append(self, x):
        super(MyList, self).append(x)
        self.altlist.append(x)

    def sortalt(self):
        ...

    ...
0
source

All Articles