[Tutor] Dynamic creation of class instances...

Bryce Embry bryce@bembry.org
Fri, 24 May 2002 14:55:34 -0500


Howdy,
I'm one of the folks who asked a question along the lines of dynamic class 
creation. Coming from a very limited programming background I don't have a 
strong grasp of OOP, and I haven't had to do any work in a language where 
all variables must be declared before use (ala C++). I asked the question 
largely because of my understanding (or misunderstanding) of classes and OOP.

I think this misconception comes from the examples of classes given in a 
lot of texts. The examples given in most of the introductory texts are 
fairly shallow and leave the impression to newbies like me that a class is 
something you define generically so that you can create specific instances 
of it at runtime. So, I define a generic "snake" class, and then the user 
can specify more details about his snake named "Slytherin" and we'll save 
that as a new instance of the class at runtime. Or I define a generic bank 
account class, and each user will create his own instance of the bank 
account at runtime. This was my concept for a while, and I'm only now 
starting to move beyond it. Newbie programmers have heard a lot about OOP 
and are anxious to be "in the know" and use it. Introductory texts cover 
classes, but their examples are necessarily short, often very artificial, 
and thereby give a wrong impression.

As I learn more about classes it seems that the programs / scripts that I 
have been writing are just too simplistic to really use classes. I can see 
that they would be very useful in a larger project, but for my dinky 
100-line codes, it just seems that classes are not quite as practical. I 
realize there may be some dinky codes that could benefit from OOP and 
classes, but I guess I just haven't gotten there quite yet.

I think OOP was one of my first hang-ups with a lot of the Tkinter 
tutorials, too. Most of the tutorials made their GUI programs using 
classes, but only had one instance of the class. I don't understand how 
that's an improvement. I understand how a Label() is a subclass of a 
Widget(), but I don't understand why I have to create a class just to make 
a GUI. I guess that's why I wrote my own tutorial without using classes for 
Tkinter.

I guess my suggestion, then, is to work on the introductions to OOP and 
classes. I read quite a few of them when I was trying to figure out classes 
(Alan Gauld's, Lutz and Ascher's, Deitel's, DevShed's, and a few others) 
and I still didn't quite get it. I really wanted to get it, though, so I 
could be a cool programmer and use OOP. I've started working through some 
of the recommended reading posted a couple months ago, though, and I 
realize that I can be a cool programmer even if I just use functional 
programming most of the time. I like that. I grok functional programming.

Also, contrary to what a number of folks have suggested, I was not led to 
the dark side via the exec() function. This function is not really covered 
in beginning tutorials and I had not heard about it until folks on this 
list mentioned it. I doubt that it is the primary culprit leading folks 
down the slippery slope of dynamic class creation. I would tend to blame 
the examples of OOP in texts.

Well, those are my thoughts. Hopefully they're worth the paper their 
printed on.

Bryce Embry




Bryce Embry
Geek-Of-All-Trades / Master-Of-None

www.bembry.org
--------------------------------------------------------------------------------------------------------------------------------------------------------
Technology Coordinator for MHA/FYOS ^ 390 South White Station ^ Memphis, TN 
38117 ^ (901)682-2409
--------------------------------------------------------------------------------------------------------------------------------------------------------