[workshop] lesson 02: data objects
Phillip Harrington
workshop@cornerhost.com
Sun, 19 May 2002 19:18:52 -0400
Maybe It's the pain medication, but I died laughing when poor spot had a
horrible skiing accident. Thanks for the laughs.
Phil
----- Original Message -----
From: "Michal Wallace" <michal@sabren.com>
To: <workshop@cornerhost.com>
Cc: <workshop-lite@cornerhost.com>
Sent: Friday, May 17, 2002 5:47 PM
Subject: [workshop] lesson 02: data objects
>
> # lesson 02: data objects
>
> * A Quick Intro to Objects
>
> When I plan a new web app, I start with the data objects - objects
> whose main purpose is to hold data.
>
> What are objects? Instead of boring you with definitions, let's
> use an example.
>
> To begin, fire up a python interpreter. If you don't have
> python yet, you can download it from http://www.python.org/
>
> Since python's interpreter is interactive, you can follow
> along with the example by typing them at the ">>>" prompt.
>
>
> * My Dog Spot
>
> Let's create a simple class to represent dogs. By convention,
> we name classes with capital letters:
>
> >>> class Dog: pass
> ...
>
> The "..." means that python is waiting for more input.
> We don't have any more input (that's what "pass" means)
> so just press enter.
>
> That's all it took to create a class called Dog.
>
> A Dog class should contain information that applies
> to dogs. For example, most dogs have four legs:
>
> >>> Dog.legs = 4
>
> But suppose we want to talk about a particular dog - an
> instance of the class Dog:
>
> >>> spot = Dog()
>
> Now we have a Dog named "spot" (we name instances and
> other variables in lower case to tell them apart from
> classes). Since spot is a Dog, he should have four legs.
> To test that, we can ask python:
>
> >>> spot.legs
> 4
>
> Yep, spot has four legs.
>
> But suppose spot has a horrible skiing accident. (Don't worry,
> this is staged - no animals were hurt in the filming of this
> workshop.)
>
> >>> spot.legs = 2
> >>> Dog.legs
> 4
> >>> spot.legs
> 2
>
> As you can see, python lets us record the fact that while most Dogs
> have four legs, poor spot has only two.
>
>
> * Spot is a Data Object
>
> We said that objects are reusable chunks of data and behavior. The
> Dog class has a little bit of data (legs=4), but no real behavior. All
> you can do is instantiate it (make an instance of it).
>
> Since a Dog instance's main purpose is to store data, spot is a data
> object. If we wanted, we could record all kinds of data about spot,
> just by adding more fields to the Dog class: name, breed, color, and
> birthday might all make sense.
>
>
> * Who Let the Dogs Out?
>
> Let's say we're building a web app for the neighborhood kennel.
> We're going to use a Dog object to represent each dog. Of course,
> we can't change our program every time a dog checks in or out.
> We'd go nuts! Instead, we'll store our data objects in some kind
> of database.
>
> In an ideal world, we'd store objects in an object database.
> There is at least one object database for python, but I've
> never used it, so I can't say much about it. ( http://www.amk.ca/zodb/ )
>
> I will say that, in general, object databases are hard to query.
> Asking for dog # 5 is quick and painless, but asking "how many beagles
> are in the kennel?" usually requires actual code to loop through and
> count, and I don't like that.
>
> (I hope I'm not spreading FUD here - if I am, please let me know,
> because I would *love* to work with a really useful object
> database. LDAP servers seem to have a bit more strength in the
> object-querying department, but I don't have much experience
> with LDAP either.)
>
> Meanwhile, I use MySQL. MySQL doesn't know anything about objects, but
> it's fairly easy to write some generic routines to convert between
> MySQL records and python objects. Plus, MySQL is very fast, and gives
> us access to SQL (a powerful and somewhat standardized query
> language).
>
> We'll take a closer look at how this object-relational mapping is done
> in the next lesson.
>
> ----------
>
> (c)2002 sabren enterprises inc
> feel free to forward this to a friend!
>
> archives at: http://webAppWorkshop.com/
> discussion list: http://www.cornerhost.com/mailman/listinfo/workshop
> lessons only: http://www.cornerhost.com/mailman/listinfo/workshop-lite
>
>
>
> _______________________________________________
> workshop mailing list
> workshop@cornerhost.com
> http://www.cornerhost.com/mailman/listinfo/workshop
>