Week 1.1

Every thing in Python is an object!

Think about this

Assume you have hello.py inside week11 (look at my repo):

>> import os
>> os.getcwd()
>> import week11.hello
'Hello World'
>> execfile(week11.hello.__file__)
'Hello World'

Get it?

My point: object!

dot operator sounds familiar right?

>>> week11.hello
<module 'week11.hello' from 'week11/hello.py'>
>>> type(week11.hello)
<type 'module'>

Python: A dynamic language with strongly typed model

In C++

int main()
    unsigned int num1 = 4;
    num2 = 10000;


This will raise compilation error due to invalid declaration. Missing type! This is statically typed.

In Python

def main():

    num1 = 4
    num2 = 10000

Perfectly fine. No declaration is needed at the point where the name is used. Type is determined at run time.

This is dynamically typed.

Variables can be rebinded

In Python, everything is an object. As a dynamic language, object has types, not variables.

# C++
int num1 = 4;

# Python
num1 = 4;
num1 = "Hello"

In C++, num1 is a variable. It holds a chunk of memory whose type is an int and the value is 4.

In Python, num1 is a name, which points to an integer object first. Then, the name is rebinded to a string object.



I will go over the in-depth C-API in the later workshop.

For now, you just need to know four things:

  1. every Python object has a reference counter
  2. the object's refcounter is incremented when another name refers to it
  3. the object's refcounter is decremented when a name stops referring to it
  4. once refcounter reaches zero, at some point, garbage collector will do free()


num1 --------->  |   IntObj       |
                 | id: 3072846400L|
                 | val: 4         |
                 | refcnt: 1      |

num1 = 4

Example (2)

num1 --------->  |   IntObj       |
                 | id: 3072846400L|
                 | val: 4         |
        ---->    ------------------
       |         | refcnt: 2      |
       |         ------------------
num2  --

num1 = 4
num2 = num1

Now refcnt is 2.

Strongly Typed

Arbitary type conversions are not allowed unless implemented.


print 100 + ' is a good score'

will result in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Weakly Typed

Javascript. This messes up my robot when grading your assignment (for those taking Grossberg's Web Site Design ....)


Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help