Ausgabe bezüglich der Überlappung der Speicherzuweisung für separate DataClassesPython

Python-Programme
Anonymous
 Ausgabe bezüglich der Überlappung der Speicherzuweisung für separate DataClasses

Post by Anonymous »

Ich experimentiere mit der Definition von DataClasses zum Enthaltenden von CTypes -Objekten. Ich habe drei Klassen definiert; Zwei davon werden als Attribute zum dritten verwendet: < /p>

Code: Select all

@dataclasses.dataclass
class Point:
x = ctypes.c_int(0)
y = ctypes.c_int(0)

@dataclasses.dataclass
class Point2:
x = ctypes.c_int(0)
y = ctypes.c_int(0)

@dataclasses.dataclass
class TestObj:
point1 = Point()
point2 = Point()
Die TestObj wird dann an eine C -Funktion inc :
übergeben

Code: Select all

#include 

void inc(int *x, int *y)
{
*x = *x + 1;
*y = *y + 1;
}
< /code>
über einen Wrapper: < /p>
def inc(test: TestObj):
_inc = lib.inc

_inc.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
_inc.restype = None
_inc(test.point1.x,test.point1.y)
Bei der Untersuchung des Ergebniss des Durchgangs an INC Ich beobachte den Snippet:

Code: Select all

test = TestObj()
print(f"addressof test.point1.x = {ctypes.addressof(test.point1.x)}")
print(f"test.point1.x.value = {test.point1.x.value}")
print(f"addressof test.point1.y = {ctypes.addressof(test.point1.y)}")
print(f"test.point1.y.value = {test.point1.y.value}")
print(f"addressof test.point2.x = {ctypes.addressof(test.point2.x)}")
print(f"test.point2.x.value = {test.point2.x.value}")
print(f"addressof test.point2.y = {ctypes.addressof(test.point2.y)}")
print(f"test.point2.y.value = {test.point2.y.value}")
inc(test)
print("after pass to inc")
print(f"addressof test.point1.x = {ctypes.addressof(test.point1.x)}")
print(f"test.point1.x.value = {test.point1.x.value}")
print(f"addressof test.point1.y = {ctypes.addressof(test.point1.y)}")
print(f"test.point1.y.value = {test.point1.y.value}")
print(f"addressof test.point2.x = {ctypes.addressof(test.point2.x)}")
print(f"test.point2.x.value = {test.point2.x.value}")
print(f"addressof test.point2.y = {ctypes.addressof(test.point2.y)}")
print(f"test.point2.y.value = {test.point2.y.value}")
< /code>
führt zu: < /p>
addressof test.point1.x = 124872721403536
test.point1.x.value = 0
addressof test.point1.y = 124872719995280
test.point1.y.value = 0
addressof test.point2.x = 124872721403536
test.point2.x.value = 0
addressof test.point2.y = 124872719995280
test.point2.y.value = 0
after pass to inc
addressof test.point1.x = 124872721403536
test.point1.x.value = 1
addressof test.point1.y = 124872719995280
test.point1.y.value = 1
addressof test.point2.x = 124872721403536
test.point2.x.value = 1
addressof test.point2.y = 124872719995280
test.point2.y.value = 1
Die Speicherorte der nachfolgenden X bzw. Y jedes Punkt Instanz teilen denselben Speicherort und die X und y Attribute jedes Punktes. testObj als:
neu definieren

Code: Select all

@dataclasses.dataclass
class TestObj:
point1 = Point()
point2 = Point2()
und übergeben Sie es erneut an den Inc Wrapper Ich beobachte das beabsichtigte Ergebnis von:

Code: Select all

addressof test.point1.x = 133142487043728
test.point1.x.value = 0
addressof test.point1.y = 133142485635472
test.point1.y.value = 0
addressof test.point2.x = 133142485635728
test.point2.x.value = 0
addressof test.point2.y = 133142485635984
test.point2.y.value = 0
after pass to inc
addressof test.point1.x = 133142487043728
test.point1.x.value = 1
addressof test.point1.y = 133142485635472
test.point1.y.value = 1
addressof test.point2.x = 133142485635728
test.point2.x.value = 0
addressof test.point2.y = 133142485635984
test.point2.y.value = 0
Während die Attribute von x und y von Punkt1 und point2 dieselben Speicherorte repostiven teilen, sind nur noch der Punkt 1 x und y erhöht. y Attribute teilen scheinbar denselben Speicherort, aber die Änderung des Namens des Punktes liefert das beabsichtigte Ergebnis. Ich schätze alle, um zu verstehen, was passiert.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post