OO-programming question

Grant Griffin g2 at seebelow.org
Wed Sep 6 03:29:17 EDT 2000


Aahz Maruch wrote:
> 
> [some snipping, much quoting]
> 
> In article <39B3F7D9.C0D540D0 at seebelow.org>,
> Grant Griffin  <g2 at seebelow.org> wrote:
> >
> >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.
> 
> Well, no, each variable does *NOT* have an underlying type, except
> insofar as *every* variable has the same underlying type: "reference".
> What has type in Python is objects, and variables are references to
> objects.  Therefore variables cannot have type.

You forgot the "QED" <wink>
...
> 
> That's why experienced Python programmers almost always speak of
> variable binding rather than "setting the variables value".

I dunno...

This whole nutty reference thing is important to understand for the
cases it comes into play, but in many cases it falls under the category
of something that is "going on under the hood", that can be safely
ignored.  Then, the old-fashioned and more intuitive concept of "setting
a variable's value" works just fine.

I don't know about you, but I don't have enough spare brain cells to
think about "variable binding" any more than I have to.  Of course, it's
a useful and powerful concept, but I try to write code that doesn't
depend on it any more than necessary.

if-something's-hard-to-explain-it's-probably-a-bad-idea-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