Click to See Complete Forum and Search --> : Objects & Classes in Python


Benny B
07-28-2001, 10:51 PM
Hi,

I've been trying to learn Python, but can't get my head around Objects and Classes.....

What exactly are they used for and how are they usefull??

I've been through the Python tutorial, but I still need further clarification on the subject......if anyone could help out it would be greatly appriciated.

Thanks....

jemfinch
07-29-2001, 01:08 AM
Have you ever done Object Oriented Programming before?

If not, it might take a bit to get your head around it.

In normal (imperative) programming, you break down a problem into procedures, and then call the proper procedures with the proper data to solve the problem.

In Object Oriented Programming, you break down a problem into the objects involved in that problem, and define what behavior those objects have. Then you use those objects to solve your problem.

I'm sure some other people here can expound on those ideas, and I'd love to see how they define the difference.

Anyway, one advantage of using objects is that it allows more genericity in your programming. For instance, look at a file object in Python. It has a certain interface, defined by the methods (functions) it presents to the world. Things like "read", "readline", "readlines", "seek", and so on. What's cool about objects is that functions that take a file object can take any object that satisfies the file object interface. For instance, Python comes with a module "StringIO" that has a StringIO object that acts like a file (ie, it presents the same interface) but in reality is implemented as a string. There are several good reasons and uses for such an object.

When a programming language presents a new paradigm in programming, don't expect to be able to learn it immediately. It'll take some time to really get the hang of it. Once you've got basic Python syntax down, start writing programs and reading others' programs in it and you'll soon become a l33t Python programmer like myself and Ben Briggs :) You'll also, incidentally, see lots of uses for objects.

Jeremy

Benny B
07-29-2001, 01:32 AM
Thanks for the reply Jemfinch,

Have you ever done Object Oriented Programming before?

Nup, probably why it (objects, classes) seems so confusing...

When a programming language presents a new paradigm in programming, don't expect to be able to learn it immediately. It'll take some time to really get the hang of it. Once you've got basic Python syntax down, start writing programs and reading others' programs

Yep, I'm doing exactly that....thanks for the explaination, things are still pretty unclear, but like you said, I'll just have to keep at it and soon it should click......hopefully :)

Thanks....

Stuka
07-29-2001, 10:17 PM
My best (to date) explanation of objects is based on a car. A basic "car" object can be told to do certain things through its interface: accelerate, decelarate, turn, etc. Now, if you were programming this in an imperative language, you would need functions to do this, and if you had more than one car, each function would need to know which car it was operating on. With object oriented programming, you define a car class, with methods for doing all the necessary operations. When you instantiate an object of this class, you simply tell that object, through its methods, what to do. If you have a second car, you create it, then tell it what to do through its methods (which are identical to the first car's, but know that they belong to the second). Hope this helps a bit ;)