4

When i say,

>>>radius = 2

Internally, a new object by name radius is created in __main__ module's global frame and this object is of class int. Reference variable is also created by name radius which points to that object.

When i say,

>>>from math import sqrt

Internally, a new object by name sqrt is created in __main__ module's global frame and this object is of class function. Reference variable is also created by name sqrt which points to that object of function type.

when i say,

>>>def square(x):
         return mul(x,x)

Internally, a new object by name square is created in __main__ module's global frame and this object is of class function. Reference variable is also created by name square which points to that object of function type.

My question:

Is my understanding correct?

or

Does radius and sqrt and square objects are created outside __main__ module's global frame and reference variables(radius sqrt square) sitting within __main__ module's global frame point to these objects?

In CS61A Fall 2012 course, the teacher says, this is how it is, which looks like hiding many details of python program memory model.

program environment in memory

14
  • You should probably read this and/or this. Commented Jul 3, 2014 at 12:44
  • 2
    Objects don't have names, they only have references.So, saying "a new object by name radius is created" is wrong. Read: A Word About Names and Objects Commented Jul 3, 2014 at 12:47
  • i just renamed 'space' to 'frame' in my query, as their are two frames, one global frame till the python program exits and local frame for every function call in python's program environment(memory). Commented Jul 3, 2014 at 12:58
  • No. This simply creates a name radius (not an object) which references the same int object referenced by the literal 2. Commented Jul 3, 2014 at 12:59
  • 1
    @200OK: I believe calling them names is better than calling them references. Since variables having immutable types are definitely not references. Commented Jul 3, 2014 at 13:11

2 Answers 2

2

It's kind of moot to ask where an object is created vs where a reference lives. As 200 OK points out in the comments, all object names are references - there is not a name which is more canonical or native than the rest. Objects simply exist (on a private heap, although that is for all intents and purposes irrelevant), and names point to them.

That said, there is one nuance that is not quite right in your question. When you import something from a module - whether it's the entire module itself, or just a function - a reference to that module is stored in the sys.modules dict. That ensures that when another part of the code imports from that module, it is not re-imported; the existing imported version is used.

Sign up to request clarification or add additional context in comments.

16 Comments

Why do you call them references? If you say a = 2 then b = a it's not as if b is a reference to a nor is it a reference to the same object since a += 2 has no effect on b. Isn't it better to call them names? The same object has two names.
i do not understand(picturize) sys.modules dictionary so i can't picturize yet how any simple python program will look in memory and point to other modules/functions when we try pointing it. Can you please picturize(in a diagram) any sample(simple) python program( in context to my query) that points to different module? That would make my life simpler.
@NeilG I'm not sure I understand your distinction. They are names which refer to things. You have to use some verb there: you could say they "point" to things, but pointer is an even more overloaded term than reference in this context.
@overexchange why do you care? Seriously? Python manages your memory for you; it is not C.
@overexchange: Right, just like my first comment…
|
0

Internally, a new object by name radius is created in __main__ module's global frame and this object is of class int.

You are conflating object creation and binding (assignment) of references to objects. The statement radius = 2 causes...

  • evaluation of the expression 2, which may or may not create an object (in CPython it doesn't because small integers are cached), then
  • binding of the name radius to the object (value) that the evaluation produced.

Similarly, in the import case, the import may or may not create an object. If you imported math before, its objects are already created. You're just assigning a new name, sqrt, to an object that was created by importing the math module. Asking whether an object resides in a module or not makes no sense, because objects don't live in modules. They may be referenced by modules or created by imports, but afterward, they just in a single big, flat heap space. It's the references to them that form a structured namespace.

def is special in that it causes both evaluation and binding of a name to a function object.

Comments

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.