Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Philip Schultz
Philip Schultz
11,437 Points

How does memory work in Python?

Hello everyone, I'm finding it difficult to understand what exactly is happening when we make variables and copy variables in python (and list, dictionaries, etc), also testing them in conditional statements. After doing some research on my own I think I narrowed my question down to be "What is the difference between passing by reference and passing by value". Can someone show me an example of what exactly the difference is between the two and what is going on in the background with memory? Also, How do you know when to use the double equal or the 'is' and 'is not' operators in conditional statements? Are there major differences between high-level languages in regards to this, or are they all the same.

1 Answer

Steven Parker
Steven Parker
231,275 Points

The concepts of "passing by reference" and "passing by value" are used in other languages but don't really apply to Python. Arguments in Python are "passed by assignment" which can appear to be either of the other types depending on the circumstances.

The important concept in Python is that everything is an object, and objects have both identity and value. Identity is essentially the storage location in memory. The "is" operator compares identity and the "==" operator compares value.

Here's an example function that uses both tests to compare two things:

def compare(a,b):
    if a is b:
        print("a and b are the same object")
    elif a == b:
        print("a and b have the same value")
    else:
        print("a and b are completely different")

So, using that function you can show the difference between equality and identity:

>>> a = 12345
>>> b = a
>>> compare(a,b)
a and b are the same object :point_left: identity
>>> b = 12345
>>> compare(a,b)
a and b have the same value :point_left: equality

And this is not typical of other languages.

Philip Schultz
Philip Schultz
11,437 Points

Steven, I thought I understood, but now I'm confused again. I've been playing around in the python shell and I found some very weird things. I found that if I have two strings that are equal then they are considered the exact same object, but only if they don't have any spaces in them. For example.

string1 = 'hello'
string2 = 'hello'
string1 is string2 # this will return true, so it must be the same object in memory. 

However, if I make a string with a space in it, they are not considered the same object. 
for example
```python
string1 = 'H I'
string2= 'H I'
string1 is string2 # this will return false

What is going here? Also, for numbers.

num1 = 5
num2 = 5
num1 is num2 # this will return true; same object

#However, the code below will return false
num1 = 800
num2 = 800
num1 is num2 # this will return false

Why are equivalent strings and numbers the same object, but only up to a certain point?

Steven Parker
Steven Parker
231,275 Points

Now you're seeing the effects of internal optimizations. Small integer values (256 or less) and some strings will be combined and share memory. It can do this safely because numbers and strings are both immutable.

I don't know enough about how the optimizer combines strings to explain why it doesn't do it when the string has spaces. Just be aware that Python does do this sometimes in an effort to be efficient.

"Pay no attention to that man behind the curtain!" :wink: