[Tutor] How the shell relates to programs in idle

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 25 19:59:02 EST 2020


On 25/02/2020 23:04, Alan Gauld via Tutor wrote:
> On 25/02/2020 14:48, daniel rieder wrote:
> 
>> when a program is run, the variables ... created in a program that is run from idle are retained
> 
> We need to be careful about the terminology here.

Actually, reading it back, I was being sloppy with
terminology myself.

Below is the same answer but expanded in places
to correct my own misinformation! Sorry.

> When you run a program in IDLE it is executed in the IDLE shell.
> When the program stops the shell still runs so any data objects created
> by the program will still exist and you can inspect them. But only the
> objects that still live at the end of the program, not all the objects
> that the program created.

Notice that I use the word object  here (or you could use
'value' if you prefer). Python has a clear distinction
between variable names and values. A variable in Python
(unlike most other languages) is just a name which refers
to an object(or value). The same object can be referred
to by multiple names (and a single name can refer to
multiple values over its lifespan)

> Here is the key point.
> 
>> def main():
>>
>>     dictionary = createDictionary()
>>     print(dictionary['two'])
>>     print(dictionary['red'])
> 
> When you create an object inside a function the object
> only exists within that function, unless you return it
> to the caller of the function.

Actually that should say "when you create a variable
name inside a function the name only exists within
the function.

Now we need to talk about objects and their lifetimes.
When an object is created  it exists for as long as
a name refers to it. Thus an object created inside
a function will die when the variable inside the
function dies unless another name refers to it.
This is true when the object is returned from the
function to the caller, provided the caller assigns
it to a value. So returned values live on after the
function that created them ceases to run.

Here is a short example:

def create999():
    x = 999    # create a new name, x and value
    return x   # returns the VALUE of x to the caller

def create7000():
    x = 7000   # create a new name, x and a value
    return x   # returns the VALUE of x to the caller

def main():
    y = create999()
    print(y)
    print (create7000())
    print(x)   # error! x is not known inside main.

Note that main() prints both values successfully, but the 7000 value
gets destroyed immediately after the print because it was not assigned
to a name (ie a variable). The only way to get 7000 printed again would
be to call create7000() again.

However, the 999 value is stored in a variable named y and so we can
print it out as often as we want without calling create999 again, simply
by calling print(y).

If we try to print x we get an error because x only exists inside the
create functions. It is destroyed when the functions end.

So it is the object/value that is returned from the function not the
name. And the object is only stored if it is assigned to a variable
(such as y) in the calling function.

> So when you call createDictionary() you create spanish
> inside the function. But because you return spanish,
> the object is passed out to the caller(main) when
> the function terminates. So  the dictionary is now referenced
> by the variable name 'dictionary' which exists inside main.
> 
> But because you don't return dictionary to a variable
> outside main, it dies when main ends.
> 
> So when the program finishes and you try to access the
> data it is no longer there. The only objects left are
> the two functions createDictionary() and main().

Note that Python treats functions as objects too - this
is an advanced concept you can ignore for now!


>> Traceback (most recent call last):
>>   File "<pyshell#10>", line 1, in <module>
>>     print (spanish['two'])
>> NameError: name 'dictionary' is not defined
> 
> That's because the dictionary variable only exists inside main().
> You cannot see it outside of main.

Note that it was a NAME error not a Value error.
It was the name 'dictionary' that it did not recognise.
So even if the dictionary object had been returned it
would need to be referenced by whatever name the receiver
used. Not the name inside main.

I hope that has clarified rather than further confused matters!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list