# First, create a dictionary:

dictionary = {}

# Then, create some instances of classes in the dictionary:

dictionary["DoubleSquare 1"] = DoubleSquare(5)

dictionary["long rectangle"] = Shape(600,45)

#You can now use them like a normal class:

print dictionary["long rectangle"].area()

dictionary["DoubleSquare 1"].authorName("The Gingerbread Man")

print dictionary["DoubleSquare 1"].author

As you see, we simply replaced our boring old name on the left-hand side with an exciting, new, dynamic, dictionary entry. Pretty cool, eh?

Conclusion

And that is the lesson on classes! You won't believe how long it took me to write this in a clear-cut manner, and I am still not completely satisfied! I have already gone through and rewritten half of this lesson once, and if you're still confused, I'll probably go through it again. I've probably confused some of you with my own confusion on this topic, but remember - it is not something's name that is important, but what it does (this doesn't work in a social setting, believe me... ;)).

Thanks to all,

sthurlow.com

Modules

Introduction

Last lesson we covered the killer topic of Classes. As you can remember, classes are neat combinations of variables and functions in a nice, neat package. Programming lingo calls this feature encapsulation, but reguardless of what it is called, it's a really cool feature for keeping things together so the code can be used in many instances in lots of places. Of course, you've got to ask, "how do I get my classes to many places, in many programs?". The answer is to put them into a module, to be imported into other programs.

Module? What's a Module?

A module is a python file that (generally) has only defenitions of variables, functions, and classes. For example, a module might look like this:

Code Example 1 - moduletest.py

### EXAMPLE PYTHON MODULE

# Define some variables:

numberone = 1

ageofqueen = 78

# define some functions

def printhello():

print "hello"

def timesfour(input):

print input * 4

# define a class

class Piano:

def __init__(self):

self.type = raw_input("What type of piano? ")

self.height = raw_input("What height (in feet)? ")

self.price = raw_input("How much did it cost? ")

self.age = raw_input("How old is it (in years)? ")

def printdetails(self):

print "This piano is a/an " + self.height + " foot",

You've reached the end of published parts.

⏰ Last updated: Oct 04, 2008 ⏰

Add this story to your Library to get notified about new parts!

python tutorialsStories to obsess over. Discover now