Archive - Originally posted on "The Horse's Mouth" - 2006-10-23 04:13:29 - Graham Ellis
From a most interesting Saturday which was spent doing a one on one session on thread, wxPython, etc - some Python snippets that provide unusual demos and hard-to-find answers:
The Backtic operator evaluates an expression and returns the result as a string
val1 = 16
val2 = 18
result = "The result is "+`val1 + val2`
print result
If you want to have various bits of code running at the same time you can use piping, or you can use threads, or you can use a client / server system via TCP/IP.
We have an example of Python threading and a python tcp/ip client and a simple python server all available on this web site. Piping is especially useful if you're containing / running an outside process on your machine, threads are useful where you're processing the same type of data in parallel many times over, and a client / server architecture is ideally suited to a logger and display application, with a server logging and one or more client, perhaps on remote computers, displaying.
The wxPython module provides an excellent GUI - in other words an excellent way to provide a user interface on the front of your Python code. However, there's a lack of good worked learning examples. I've just added more source code examples - linkable from our wxPython training course module. These examples were originally based on other published code, but have been extensively rearranged for clarity and commented.
The super call only works on new style classes!.
If you try to use "super" on old syle classes, you'll be told "TypeError: super() argument 1 must be type, not instance".
Old style classes ...
# old style classes
class thing:
def __init__(self):
pass
class specialthing(thing):
def __init__(self):
thing.__init__(self)
this = specialthing()
New style classes ...
# new style classes
class thing(object):
def __init__(self):
pass
class specialthing(thing):
def __init__(self):
super(specialthing,self).__init__()
this = specialthing()
What are new style classes? .... they're classes that are derived, directly or indirectly, from built in classes and they've been around since Python 2.2. As well as super, other new features include the ability to define static and class methods, rather than just a method in the same namespace. more details (external link)
We offer Python training courses. If you've just one or two people to be trained, our public courses which run every 2 or 3 months are going to be best value. For slightly larger groups, we'll run a private course at our centre. With 6 or more delegates all wanting to learn the same material at the same time, we'll come to you - you just provide a room and the students, we do the rest!