Coding standard: Prefixing variables to indicate datatype
John Machin
sjmachin at lexicon.net
Thu Jan 16 16:31:48 EST 2003
"Simon Burton" <simonb at webone.com.au> wrote in message news:<pan.2003.01.16.09.42.41.751206 at webone.com.au>...
> On Thu, 16 Jan 2003 10:28:42 +0100, 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.
>
> So use a different variable for each incarnation.
Indeed. The only justification (thankfully of rare application these
days) that I've found for re-use of variable names, even of the same
type, has been when writing heavily used C code for a register-starved
not-very-orthogonal architecture (e.g. 8086) and a compiler with a
brain-starved register allocator.
>
> Just wrote this:
>
> def capit(s):
> l = s.split("_") # could re-use s here
> l = [ s.capitalize() for s in l ]
> return string.join(l,"")
>
> capit("some_name") returns "SomeName"
>
> regards,
> Simon Burton.
1. What is the point of reusing variable names when the function could
be a one-liner and still quite legible:
def capit(s):
return "".join([x.capitalize() for x in s.split("_")])
2. string.join()???? Are you still using Python 1.5???? Update now!!!!
3. Can you see what is wrong with this without feeding it to Python:
def capit(s):
l = s.split("_") # could re-use s here
l = [ s.capitalize() for s in 1 ]
return string.join(l,"")
4. Have you considered that capit() conflates "fubar", "_fubar",
"__fubar", etc?
More information about the Python-list
mailing list