OO-programming question

Aahz Maruch aahz at panix.com
Tue Sep 5 17:08:54 CEST 2000

[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.

Yes, it would be possible to restrict the range of types that a variable
can reference, but it would almost certainly be a Bad Idea until we can
inherit from the internal types (or at least declare a user-defined
class to be an instance of an internal type).

That's why experienced Python programmers almost always speak of
variable binding rather than "setting the variables value".
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Goodbye dinner for Netcom shell, Thurs 9/7, 7:30pm, Mountain View, CA
e-mail me for details

More information about the Python-list mailing list