0

consider the following python snippet

spoon = 2 + 2

print spoon

def spoon(x):
    return x + 3

print spoon

spoon = 'foo'

print spoon

here we are using same name to create new objects. my doubt is how can we access the data inside the first object we created. i.e., how can i access 4 which is stored in spoon. although the name space is pointing to a new id but still the old object exists so i am asking is there anyway to access the contents of those objects ?

1 Answer 1

2

You can't. Once the object becomes unreachable, it is deallocated. The interpreter may find out about this immediately if the object is subject to reference counting. In that case 4 is gone the moment you set spoon to foo.

Consider this imperfect example for demonstration

class Foo(object):
    def __del__(self):
        print "tell my wife I love her!"


>>> f = Foo()
>>> def f():
...  print 'Make room for thy beloved king!'
...
tell my wife I love her!
>>> 
Sign up to request clarification or add additional context in comments.

6 Comments

Wasn't he asking about function/variable name definitions rather than 2 same named variables?
so here when ever a new object with same name space is created the interpreter will delete all the object associated data and starts creating a new object reference and store the new contents. correct me if i am wrong
@charan: Values are like balloons, names are mere tags tied to the balloons. As soon as there are no more labels attached to a balloon, it drifts away into the sky..
@charan: If you need continued access to a balloon, make sure you have it tied down to a label somewhere.
@glglgl: I may have used the image before. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.