Skip to content

python

Guides : visit

The tutorial there may be long but very detailed, it explains for instance how functions call different symbol tables. The right kind of detail i like.

Current : visit

pass 1: quickly review, check if the sub links are part of the rest of the tutorial. pass 2: completely read each chapter and make an excerpt. pass 3: read the excerpt

The sub links in the tutorial seem to point to the library section. That's not included in the main tutorial, so add that as the next bit to read.

Introduction

Python 3 was introduced in 2008, it's about time to use it.

threading

simple version

from threading import Thread

def tick(): while (stop==False): print("Yo , bitch !") sleep(0.03) print("Tik");

ticker = Thread(None,tick) ticker.start()

subclassing

subclassing from Thread
class tick(Thread):
    def __init__(self,tid,name,counter):
        Thread.__init__(self)
        print name

    def run(self):
        global stop
        while (stop==False):
            print("Yo , bitch !")
            sleep(0.03)

Starting :

run
ticker = tick(1,"draadje 1",1)
ticker.start()

dictionary

access

dict
1
2
3
4
dict = { 'kees' : 47, 'yvon': 48 };
print dict['kees']          # yep
print dict.get('yvon')      # yep
print dict.kees             # nope

output: (dict.kees will fail, it is not javascript!!)

output
1
2
3
4
5
6
47
48
Traceback (most recent call last):
 File "./t.py", line 15, in <module>
    print dict.kees
AttributeError: 'dict' object has no attribute 'kees'

traversal

traversal
#!/usr/bin/python

dict = { "kees" : 47, "yvon": 48 };

for n in dict.keys():
    print n

for v in dict.values():
    print v

for n,v in dict.items():
    print ( "%s is %d" % (n , v));

Output:

output
1
2
3
4
5
6
yvon
kees
48
47
yvon is 48
kees is 47

exceptions and debugging

You won't always get a command line error, for instance when using threads :

exceptions
def startMove(self):
     try:
         fmt = self.code + ":"
         print (fmt) // without catching this line gets printed
         fmt += "i" + self.startx + "," + self.starty;  // can't concat str + int
         print (fmt) // this line will not !! so the above line has an error
     except AttributeError as e: // you will need to exactly catch that error
         print ("got : " + e) // note that this again is an exception
     except Exception as ee: // or the most general one and print it
         print ("Got an exception : ") // cant concat string and Exception
         print (ee) // so separate line
     except: // or at least say that something went wrong
         print ("something went wrong")

static vs class vs instance

Classes and instances both have space for a variable, the instances take the class variable to initialise their own copy.

image