You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An object pointer (created by `to_ptr` or `Pointer.make_from`) points to the actual Python object (`PyObject*`), while a variable pointer points to that actual variable.
6
+
7
+
```py
8
+
hello ="123"
9
+
ptr = to_ptr(hello)
10
+
# ptr now points to "123", not "hello"
11
+
```
12
+
13
+
## Creation
14
+
15
+
You can make a variable pointer through `to_var_ptr`:
16
+
17
+
```py
18
+
from pointers import to_var_ptr
19
+
20
+
a ="hi"
21
+
ptr = to_var_ptr(a)
22
+
```
23
+
24
+
Note that of course you can't use a literal, because that isn't a variable:
25
+
26
+
```py
27
+
from pointers import to_var_ptr
28
+
29
+
ptr = to_var_ptr(123) # ValueError
30
+
```
31
+
32
+
## Movement
33
+
34
+
Moving is much different when it comes to variable pointers. With an object pointer, you are overwriting that actual value, while with variable pointers you are just changing the value of that variable.
35
+
36
+
You can use movement the same way you would with an object pointer:
37
+
38
+
```py
39
+
from pointers import to_var_ptr
40
+
41
+
my_var =1
42
+
my_ptr = to_var_ptr(my_var)
43
+
my_ptr <<=2
44
+
print(my_var, 1)
45
+
# outputs 2 1, since we didnt overwrite 1 like we would with object pointers
46
+
```
47
+
48
+
You are free to use movement however you like with variable pointers. It isn't dangerous, unlike its counterpart.
49
+
50
+
### For C/C++ developers
51
+
52
+
Movement in variable pointers is equivalent to the following C code:
53
+
54
+
```c
55
+
int my_var = 1; // "my_var = 1"
56
+
int my_ptr = &my_var; // "my_ptr = to_var_ptr(my_var)"
57
+
*my_ptr = 2; // "my_ptr <<= 2"
58
+
// my_var is now 2 (the actual 1 is unchanged, thankfully)
59
+
```
60
+
61
+
## Assignment
62
+
63
+
Assignment works the same way as it does with object pointers:
0 commit comments