Coding standard: Prefixing variables to indicate datatype

Peter Hansen peter at engcorp.com
Thu Jan 16 09:06:56 EST 2003


Thomas Weholt wrote:
> 
> Hm ... I see your point. I began thinking about this when I read some old
> code where a variable changed datatype during its lifespan inside a function
> several times. I think it was passed as a string, turned into a int and
> could be returned from the function as None depending on its content. It was
> awful. It was probably the worst code I've ever written, in any language,
> but only Python allowed me to do it this way. If I'd prefixed the variable
> to indicate it's datatype it would have been harder to change its datatype
> that way, but then again --- changing datatype of a variable during
> processing is probably not a very good idea in the first place.

You might have a slightly different take on things if you look at it this
way.  You are not changing the datatype of the variable because, in Python,
"variables" (as in, names) do not have types.  The thing that the name 
is "bound to" is what has a type (e.g. integer, object, etc), and you
can rebind the name during the function.

Practically it might amount to the same thing.  In terms of the underlying
mechanisms it is quite, quite different.

It's also not considered inherently wrong in Python, although in the
hands of a bad programmer it can be dangerous.

-Peter




More information about the Python-list mailing list