OO-programming question

Grant Griffin g2 at seebelow.org
Mon Sep 4 21:28:25 CEST 2000


Alex Martelli wrote:
> 
> "Ing. Roman Fischer" <Roman.Fischer at teleweb.at> wrote in message
> news:B5D85A24.7654%Roman.Fischer at teleweb.at...
> > Reading "Learning Python" I have problems of understanding the advantages
> of
> > the concept of object-oriented programming in Python:
> >
> > 1. When I understand it wright I can add/delete items to classes AND to
> > object AFTER their definition. What is the advantage of this concept? I
> 
> Same as any other dynamic aspect -- you can easily adapt your
> program's actual structure to the problem it's handling, on the fly.
> 
...
> > 5. What is the advantage of including "self" into the paramters of a
> method?
> 
> Explicitness.  "Explicit is better than implicit" is one of Python's
> mantras.

If I were a newbie right now, I might be tempted to ask, "Then why don't
I have to declare variables--as I do in C++, which has an _implicit_
"self" parameter (a.k.a. "this"), but otherwise makes me _explicitly_
declare the types of my variables?"

The answer to my own question <<who asked ya?>>, I believe, is partly 1)
above, and partly the fact that you really _do_ sortta have to declare
variables.  Specifically, you have to "write" to a variable before you
"read" it.  And what you write to it defines its type:

	i = 1		# integer
	f = 1.0    	# floating-point
	l = []		# list

Then, certain operations make sense and some don't, given the type. 
However, later, Python's "dynamic typing" allows you to "re-declare" the
variables, e.g.:

	i = []		# list
	f = 1		# integer
	l = 1.0		# floating-point

I think Python's concept of "sortta declaring" variables is a brilliant
case of "having your cake and eating it to".  (Maybe Guido missed his
calling as a pastry chef. <wink>)  It provides part of the safety of
explicit type declarations because if you accidently misspell a variable
name and as you try to "read" it, you get an exception.  (Note, though,
that Python allows you to misspell the name throughout, so long as you
are consistent. ;-)  Also, Python's approach allows you to implicitly
declare the "type".  (Then again, isn't explicit supposed to be better
than implicit? <wink>.)  That is, even though they are not "declared",
per se, each variable still has an underlying type, so operations that
don't make sense (e.g. "appending" to an integer) can be caught as
exceptions.  Therefore, you get all the advantages of dynamic typing, as
in Perl, but with a considerable portion of the "protecting you from
yourself" advantage that comes from static typing, as in C.

if-you've-gotta-program-dynamically,-kids,-at-least-wear-some
   -protection-<wink>-ly y'rs,

=g2
-- 
_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation	      http://www.iowegian.com



More information about the Python-list mailing list