Computer Science Canada

x = x + ";" adds 2 semicolons to x?

Author:  Geminias [ Tue Feb 17, 2009 3:58 pm ]
Post subject:  x = x + ";" adds 2 semicolons to x?

Python:

        for i in range(len(self.functions[0].b)):
            self.functions[0].b[i] = (self.functions[0].b[i] + ";")
            DebugOut(self.functions[0].b[i])


Seriously... why does self.functions[0].b[i] end with 2 semicolons although I am only adding one. I am also sure there is no semicolon originally. But for some reason it actually adds 2 although I am clearly saying to only add 1.

This has me baffled.

Author:  Zeroth [ Tue Feb 17, 2009 11:44 pm ]
Post subject:  Re: x = x + ";" adds 2 semicolons to x?

There's absolutely no reason why it would do that. What is in the variable named 'b'? If they are strings, then there should be no reason for that. If they're objects masquerading as strings, maybe there's a bug in some of the overridden functions?

Of course, maybe your debugout function is adding the semi-colons itself?

Without the rest of the code, we really can't diagnose the problem.

code:

>>> a=range(10)
>>> for i in range(len(a)):
...     a[i]=(str(a[i])+';')
...
>>> a
['0;', '1;', '2;', '3;', '4;', '5;', '6;', '7;', '8;', '9;']

Author:  [Gandalf] [ Wed Feb 18, 2009 1:00 am ]
Post subject:  RE:x = x + ";" adds 2 semicolons to x?

Indeed, just for the sake of double checking... Assuming self.functions[0].b is a list of strings, the following is an exact replica of your code without the DebugOut() calls.
Python:
a = ["hello", "hi", "bye"]
for i in range(len(a)):
    a[i] = (a[i] + ";")

Quote:
['hello;', 'hi;', 'bye;']

Author:  octopi [ Wed Feb 18, 2009 1:13 am ]
Post subject:  Re: x = x + ";" adds 2 semicolons to x?

I would try adding a "" to the end, or another value such as "X", and seeing if you receive the proper output. If it is part of an object it could be that it does some extra stuff which is adding an extra semicolon when you change the value.

Author:  wtd [ Wed Feb 18, 2009 1:30 am ]
Post subject:  Re: x = x + ";" adds 2 semicolons to x?

Zeroth @ Wed Feb 18, 2009 12:44 pm wrote:
There's absolutely no reason why it would do that. What is in the variable named 'b'? If they are strings, then there should be no reason for that. If they're objects masquerading as strings, maybe there's a bug in some of the overridden functions?

Of course, maybe your debugout function is adding the semi-colons itself?

Without the rest of the code, we really can't diagnose the problem.

code:

>>> a=range(10)
>>> for i in range(len(a)):
...     a[i]=(str(a[i])+';')
...
>>> a
['0;', '1;', '2;', '3;', '4;', '5;', '6;', '7;', '8;', '9;']


I think you meant:

code:
>>> map(lambda x: str(x) + ";", range(10))
['0;', '1;', '2;', '3;', '4;', '5;', '6;', '7;', '8;', '9;']
>>>

Author:  Zeroth [ Fri Feb 20, 2009 10:17 am ]
Post subject:  Re: x = x + ";" adds 2 semicolons to x?

wtd @ Tue Feb 17, 2009 10:30 pm wrote:
Zeroth @ Wed Feb 18, 2009 12:44 pm wrote:
There's absolutely no reason why it would do that. What is in the variable named 'b'? If they are strings, then there should be no reason for that. If they're objects masquerading as strings, maybe there's a bug in some of the overridden functions?

Of course, maybe your debugout function is adding the semi-colons itself?

Without the rest of the code, we really can't diagnose the problem.

code:

>>> a=range(10)
>>> for i in range(len(a)):
...     a[i]=(str(a[i])+';')
...
>>> a
['0;', '1;', '2;', '3;', '4;', '5;', '6;', '7;', '8;', '9;']


I think you meant:

code:
>>> map(lambda x: str(x) + ";", range(10))
['0;', '1;', '2;', '3;', '4;', '5;', '6;', '7;', '8;', '9;']
>>>


I think I meant something readable Wink I only use maps and lambdas either A) when the normal flow controls can't do what I want well enough or B) for efficiency, which I only do after I get the code working in the first place. Wink And I still leave the old structure there in comments so people can see what it does easily. But to each their own!

Author:  wtd [ Fri Feb 20, 2009 1:19 pm ]
Post subject:  RE:x = x + ";" adds 2 semicolons to x?

One big reason I prefer Ruby to Python: Python is overly statement=oriented, and treats expressions as a necessary evil.


: