En Wed, 11 Jun 2008 04:21:03 -0300, Gabriel Rossetti
<gabriel.rosset ti@arimaz.comes cribió:
Not always. Using characters as ordinal numbers is very common in that
language. A small example:
#define FIRST 'A'
#define LAST 'Z'
#define index(letter) ((letter)-FIRST)
int accum[LAST-FIRST+1];
....
char x = some_word[0];
accum[index(x)]++;
accum is an array of 26 integers, element 0 corresponding to letter 'A'.
If one has to reproduce the exact same structure, in Python it would be:
FIRST = ord('A')
LAST = ord('Z')
def index(letter):
return ord(letter)-FIRST
accum = [0 for i in range(LAST-FIRST+1)]
x = some_word[0]
accum[index(x)] += 1
Of course, in other cases, character constants are intended to be used as
character data, so ord(...) is not the appropiate thing to do when
converting to Python. But I don't think my example above is very contrived
or uncommon - chars *are* integers from the C point of view, and some
people isn't worried too much about the distinction, specially those who
use C as their main or only language.
--
Gabriel Genellina
<gabriel.rosset ti@arimaz.comes cribió:
Gabriel Genellina wrote:
Ok, I'll file one then.
>>
>It's not so clear what's the intended usage - chars are also integers
>in C. (I prefer the current behavior, but certainly it may be wrong in
>several places).
>>
Yes, true, but if you intend to use it as an integer, wouldn't you use a
numeric value instead of a character literal?
>En Tue, 10 Jun 2008 09:44:13 -0300, Gabriel Rossetti
><gabriel.rosse tti@arimaz.come scribió:
>>
>>
>Very probably - bug reports outside the tracker are likely to go
>unnoticed or forgotten.
>>
><gabriel.rosse tti@arimaz.come scribió:
>>
>>I wanted to use the h2py.py script (Tools/scripts/h2py.py) and it
>>didn't like char litterals :
>>>
>>Skipping: PC_ERROR = ord()
>>>
>>where my *.h file contained :
>>>
>>#define PC_ERROR '0'
>>>
>>I searched the web and found a post with the same error :
>>>
>>http://mail.python.org/pipermail/pyt...er/340608.html
>>>
>>but it got no replies, I tried the fix and it works. I have the
>>following questions:
>>>
>>1) Why did it not get any attention, is something wrong with it?
>>2) If nothing is wrong, did the fix not get applied because a bug
>>report wasn't filed?
>>didn't like char litterals :
>>>
>>Skipping: PC_ERROR = ord()
>>>
>>where my *.h file contained :
>>>
>>#define PC_ERROR '0'
>>>
>>I searched the web and found a post with the same error :
>>>
>>http://mail.python.org/pipermail/pyt...er/340608.html
>>>
>>but it got no replies, I tried the fix and it works. I have the
>>following questions:
>>>
>>1) Why did it not get any attention, is something wrong with it?
>>2) If nothing is wrong, did the fix not get applied because a bug
>>report wasn't filed?
>Very probably - bug reports outside the tracker are likely to go
>unnoticed or forgotten.
>>
>>3) Isn't turning a char literal into the ordinal value not contrary to
>>what a C programmer had in mind when he/she defined it? I mean if you
>>define a char literal then in python you would have used a string
>>value :
>>>
>>#define PC_ERROR '0'
>>>
>>would become :
>>>
>>PC_ERROR = '0'
>>>
>>in python, and if you intended to use the char type for an 8 bit
>>numerical value you would have done :
>>>
>>#define PC_ERROR 0x30
>>>
>>where 0x30 is the '0' ascii hex value, so shouldn'it the line in the
>>diff (see the post) be :
>>>
>>body = p_char.sub("'\\ 1'", body)
>>>
>>instead of :
>>>
>>body = p_char.sub("ord ('\\1')", body)
>>what a C programmer had in mind when he/she defined it? I mean if you
>>define a char literal then in python you would have used a string
>>value :
>>>
>>#define PC_ERROR '0'
>>>
>>would become :
>>>
>>PC_ERROR = '0'
>>>
>>in python, and if you intended to use the char type for an 8 bit
>>numerical value you would have done :
>>>
>>#define PC_ERROR 0x30
>>>
>>where 0x30 is the '0' ascii hex value, so shouldn'it the line in the
>>diff (see the post) be :
>>>
>>body = p_char.sub("'\\ 1'", body)
>>>
>>instead of :
>>>
>>body = p_char.sub("ord ('\\1')", body)
>It's not so clear what's the intended usage - chars are also integers
>in C. (I prefer the current behavior, but certainly it may be wrong in
>several places).
>>
numeric value instead of a character literal?
language. A small example:
#define FIRST 'A'
#define LAST 'Z'
#define index(letter) ((letter)-FIRST)
int accum[LAST-FIRST+1];
....
char x = some_word[0];
accum[index(x)]++;
accum is an array of 26 integers, element 0 corresponding to letter 'A'.
If one has to reproduce the exact same structure, in Python it would be:
FIRST = ord('A')
LAST = ord('Z')
def index(letter):
return ord(letter)-FIRST
accum = [0 for i in range(LAST-FIRST+1)]
x = some_word[0]
accum[index(x)] += 1
Of course, in other cases, character constants are intended to be used as
character data, so ord(...) is not the appropiate thing to do when
converting to Python. But I don't think my example above is very contrived
or uncommon - chars *are* integers from the C point of view, and some
people isn't worried too much about the distinction, specially those who
use C as their main or only language.
--
Gabriel Genellina