C++ style const's & types in Python

Aahz Maruch aahz at netcom.com
Tue Jun 27 13:56:55 EDT 2000


In article <v3465.71261$7o1.1704126 at news2.rdc1.on.home.com>,
Tom <tom-main at REMOVEME.home.com> wrote:
>
>I'm a C++ developer learning Python.

I'm not sure I'd agree with that statement, given your insistence on
following C++ paradigms in Python.

>One is const types.  Particularly given that values are passed to
>methods by reference.  But am I missing something?  Is there any way
>in Python to specify that a value (other than a tuple) must remain
>constant?

Not really.  All-caps (e.g. DEFAULT_TIMEOUT) are sometimes used as an
indicator.  Note, however, that function defaults are pretty close to
constant.

>Also, type declarations, while a nuisance in short programs, can be
>valuable in larger programs.  It would be nice if Python names could
>optionally have their types fixed.  As a substitute, I think I will
>start using Hungarian notation to prefix my Python names in cases where
>I don't intend to dynamically change their type.  Is this stupid?  Is
>there a better alternative?

Hungarian notation is a Bad Idea even in C++ programs.  Type checking
(other than for optimization) just doesn't seem to be an issue for
experienced Python programmers.  There's a belief around here that the
reason for this is that type checking is a crutch for a class of
problems that doesn't occur naturally if you're programming "the Python
Way".

Python is all about interfaces and algorithms, not types or inheritance
(despite Python's excellent class system).  For example, if you write a
specialized disk-based merge sort function, you *know* that it will work
with any input (including user-defined classes) as long as the supports
pickle and cmp().

Here's my standard quote on the subject:  The way to build large Python
applications is to componentize and loosely-couple the hell out of
everything.
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

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

The best way to get information on Usenet is not to ask a question,
but to post the wrong information.  --Aahz



More information about the Python-list mailing list