Skip to content

Instantly share code, notes, and snippets.

@jasonpr59
Last active August 29, 2015 14:13
Show Gist options
  • Save jasonpr59/1a598d1a36e1e0a460fd to your computer and use it in GitHub Desktop.
Save jasonpr59/1a598d1a36e1e0a460fd to your computer and use it in GitHub Desktop.
Some Python code that demonstrates aliasing. This code uses some intuitive concepts: individual people and their (many) names. This code is meant to drive home the point that *a variable is just a name for an object*.
"""
The aliasing demo code.
This is meant to be entered into a Python shell, bit-by-bit. (That's
why some lines appear to do nothing-- they will produce the
representation of the object when evaluated in the REPL.)
"""
import person
# 1) Make a person.
jasonpr = person.Person()
jasonpr
# OUT: Person #1 who cannot sing and is dry.
# 2) Make another name for the same person.
jason = jasonpr
jason
# OUT: Person #1 who cannot sing and is dry.
# He's not even a copy, he's literally the same person:
jason is jasonpr
# OUT: True
# So, mutating jason is the same thing as mutating jasonpr:
jason.dump_gatorade()
jason
# OUT: Person #1 who cannot sing and is drenched.
jasonpr
# OUT: Person #1 who cannot sing and is drenched.
# Let's dry Jason off.
jasonpr.dry_off()
jason
# OUT: Person #1 who cannot sing and is dry.
# 3) Let's talk about a different Jason. (We need an example of
# *distinct* objects!)
jason_deruuulooooo
# OUT: Traceback (most recent call last):
# OUT: File "<input>", line 1, in <module>
# OUT: NameError: name 'jason_deruuulooooo' is not defined
# This other Jason doesn't exist yet. Let's create him!
# The RHS expression creates a new object! (Compare to `[1, 2, 3].`)
jason_deruuulooooo = person.Person(can_sing=True)
jason_deruuulooooo
# OUT: Person #2 who can sing and is dry.
# 4) Let's see which names refer to the same/different objects.
# Jason Derulo is a totally different person from Jason P-R.
jasonpr is jason_deruuulooooo
# OUT: False
# In the story we've been telling, we specified that jason is
# a name for Jason P-R.
jason is jasonpr
# OUT: True
jason is jason_deruuulooooo
# OUT: False
# 5) Let's CHANGE what object (i.e. person) a name refers to.
# But, Jason Derulo is much more famous than Jason P-R.
# So, from now on, we want 'Jason' to refer to Jason Derulo.
jason = jason_deruuulooooo
jason
# OUT: Person #2 who can sing and is dry.
jasonpr
# OUT: Person #1 who cannot sing and is dry.
# We successfully gave a different meaning to the name Jason.
jason is jason_deruuulooooo
# OUT: True
# This means that Jason no longer refers the person who's also called
# Jason P-R.
jason is jasonpr
# OUT: False
# 6) Just to further drive the mutation/aliasing point home:
jason.dump_gatorade()
jason
# OUT: Person #2 who can sing and is drenched.
jasonpr
# OUT: Person #1 who cannot sing and is dry.
jason_deruuulooooo
# OUT: Person #2 who can sing and is drenched.
class Person(object):
"""
Silly mutable representation of a person.
A person has two properties: whether they can sing, and whether they're dry or drenched.
Their singing ability is set at construction time. Their dryness is altered with the
dump_gatorade() and dry_off() methods. (They start off dry at construction time.)
"""
_id = 1
def __init__(self, can_sing=False):
self._id = Person._id
Person._id += 1
self._can_sing = can_sing
self._is_drenched = False
def can_sing(self):
return self._can_sing
def is_drenched(self):
return self._is_drenched
def dump_gatorade(self):
self._is_drenched = True
def dry_off(self):
self._is_drenched = False
def __repr__(self):
return ('Person #%d who %s and %s.' %
(self._id,
'can sing' if self._can_sing else 'cannot sing',
'is drenched' if self._is_drenched else 'is dry'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment