0

I was trying to demonstrate call by reference and value and what the address of the variable is in python:

x = 10

print x
10

x = id(x)

print x

3457569

I pass x to my function by reference as x is an address

def foo(d):

  print "the value of d is" ,**inverseid(d)**

foo(x)

3457569

I want the inverse(d) to print 10 but it prints the address!

Is there an inbuilt inverse of id() or can someone show me how to generate the value of an address similar to the * operator in C ?

Timothy Lawman
  • 2,194
  • 6
  • 24
  • 33
  • 3
    The fact that `id()` returns a memory address is a *implementation detail* of the CPython implementation. You should not treat it as such. – Martijn Pieters Feb 22 '13 at 14:15
  • 1
    Variables don't have addresses or `id()`s. Objects do. –  Feb 22 '13 at 14:16
  • 1
    Python is not C, it operates at a level abstracted from the direct machine interface. Trying to get an object given its `id()` value is like trying to look up a chapter in a book with no table of contents. The only way to do it is to go through the book one page at a time, inspecting each page to see if the chapter you are looking for starts on it. – Silas Ray Feb 22 '13 at 14:20
  • 1
    @sr2222 With the added "fun" that it's easy to go through the pages of a book, but very hard to impossible (the closest I know is implementation-defined and unreliable) to enumerate all objects in a Python process. –  Feb 22 '13 at 14:30
  • The point was about demonstrating call by ref and value to beginners and wether it is possible in Python without reverting to VBor Pascal. – Timothy Lawman Feb 22 '13 at 20:04

0 Answers0