0

I am working in replicating some things in C type languages for class to show most things are easy to replicate overall. I am trying to work with pointers but obviously thats not something readily available. Doing something like this can work though:

class point:
def __init__(self, obj):    self.obj = obj
def get(self):              return self.obj
def set(self, obj):         self.obj = obj

I can then do something such as

a = point(5)  
b = a

And use that in a similar fashion to pointers

But now I'm trying to see if the value of a variable can be set from the id of another. Here is some code that obviously doesn't work but is my train of thought.

a = 124
b = *(int*)(hex(id(a))
(b == a) //True

So if I have the address, is it possible to do something like that

Reece
  • 59
  • 7
  • 2
    ["Besides, the determined Real Programmer can write FORTRAN programs in any language."](http://web.mit.edu/humor/Computers/real.programmers) – TigerhawkT3 Nov 29 '16 at 22:38
  • 1
    1. It's an implementation detail that the `id()` of an object is its address. 2. Even if you have an address, you have no guarantee that there is a valid object at that address. 3. `b = a` already does what you want: takes the pointer in `a` and puts it in `b`. (All Python variables are essentially pointers, after all.) – kindall Nov 29 '16 at 22:40
  • Well references but oh well. I am saying if for some magical reason I could only get the ID of a variable. Getting the variable at that address isn't possible? – Reece Nov 29 '16 at 22:44
  • Why go from the variable to its ID which might be its address back to the variable, when you already had the variable? – TigerhawkT3 Nov 29 '16 at 22:45
  • 2
    Possible duplicate of [Python: Get object by id](http://stackoverflow.com/questions/1396668/python-get-object-by-id) – juanpa.arrivillaga Nov 29 '16 at 22:45
  • I am not too interested in actually using this. I just want to find if it is possible to actually perform this – Reece Nov 29 '16 at 22:46
  • I'm not sure what you believe you are achieving with your `Point` class that couldn't be achieved using any of the builtin Python objects (i.e. *everything*). – juanpa.arrivillaga Nov 29 '16 at 22:46
  • @Reece See thing link to the dupe. The answer is that it is possible, but it is super hacky and depends on an implementation detail of CPython. In particular, look at this answer: http://stackoverflow.com/a/15702647/5014455 – juanpa.arrivillaga Nov 29 '16 at 22:47
  • @Reece Oh, and please don't write getters and setters for Python classes. – juanpa.arrivillaga Nov 29 '16 at 22:49
  • @juanpa.arrivillaga - Check out the `@property` decorator. :) – TigerhawkT3 Nov 29 '16 at 23:13

0 Answers0