Skip to content

Instantly share code, notes, and snippets.

View jasonpr59's full-sized avatar

Jason P-R jasonpr59

View GitHub Profile
@jasonpr59
jasonpr59 / scope-examples.md
Last active August 29, 2015 14:13
Scope Examples

Scope Explanation

(This Gist can be accessed at http://goo.gl/ZYAZkC.)

Introduction

In Python we use variable to refer to objects. In other words, a variable is a name for an object.

This Gist is an explanation of how Python creates variable names, and how it decides what object a variable name refers to.

@jasonpr59
jasonpr59 / alias_demo.py
Last active August 29, 2015 14:13
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
kernel: file format elf32-i386
Disassembly of section .text:
80100000 <multiboot_header>:
80100000: 02 b0 ad 1b 00 00 add 0x1bad(%eax),%dh
80100006: 00 00 add %al,(%eax)
80100008: fe 4f 52 decb 0x52(%edi)
@jasonpr59
jasonpr59 / aliasing.md
Last active August 29, 2015 13:55
An explanation of aliasing for Python. (The application to LA for 6.00 prompted me to explain a Python concept that I initially found challenging. This was my response.)

I'll explain the relationship between objects and names. That is, I'll explain aliasing.

In Python, we create objects and give them names. It's important to understand objects, names, and their relationship to each other-- at the very least it will help you avoid some bugs!

We create objects to represent data. For example, when we execute a = [4, 25], Python sees the [4, 25] and creates a list object.

When we create an object, we want to be able to refer back to it later. So we make a name that points to it. Above, we made the name a and pointed it at a list object. Then, when we use that name in a statement, Python finds the object that it points to so we can do things with the object-- like printing its value or modifying its data. For example, to execute a.append(400), Python looks at the name a, finds the object that it points to (the list), and then modifies the list by appending the value 400 to it.

We're allowed to have multiple names point to a single object. For example, we c