Thank you for the clarifications. Would you mind showing how to do the __getitem__ an __setitem__ thingie. I remember I tried it but could not get it to work with multiple arguments.
Regarding join, thank you I learned something new. It seems it's a matter of perspective. Though I'd express array.join(string) as "ask the array to join itself using string as a seperator" ;-)
The int(not a[i] == b[j]) though is something I'll never allow in my programs. I like to distinguish between false, true and numbers and the above is certainly golfish.
Brian:
Both
# Missing the [i,j] operator
and
# Missing the [i,j]=v operator
is false, they can be implemented with __getitem__ and __setitem__.
Joins of lists are usually not done with an imported string module but with "sep".join(list) wich makes some sense if you think of it. "With this string join this list" instead of as in Ruby "with this list join with this string".
if a[i] == b[j]:
. cij = 0
else:
. cij = 1
Could instead be written as int(not a[i] == b[j])
__getitem__ and __setitem__ get their arguments as a tuple. l[2,3] will call l.__getitem__((2,3))
What's wrong with converting a bool to int if it makes the code cleaner and easier to read?
Also, the to_s method should probably be replaced with __str__ instead, wich will be called by str(object). The reason of the usage of these functions to help functional programming, for instance: map(str, someCollection) wich i Ruby would be: someCollection.map{ |obj| obj.to_s }
Regarding the bool to int conversion it is my opinion that a boolean is a type with a domain of {false, true} and I do not have an implicit mapping from {false, true} to int. It is as sensible for false to map to -1 as it is to map to 0. The same holds for true. So I do not want to rely on this arbitrary mapping wich seems more like a coincidence than a resonable idea to me. _Join the equal rights for zero movement_
Why should 0 be false even if it is a completely normal number?
Comment
Sebastian:
Thank you for the clarifications. Would you mind showing how to do the __getitem__ an __setitem__ thingie. I remember I tried it but could not get it to work with multiple arguments.
Regarding join, thank you I learned something new. It seems it's a matter of perspective. Though I'd express array.join(string) as "ask the array to join itself using string as a seperator" ;-)
The int(not a[i] == b[j]) though is something I'll never allow in my programs. I like to distinguish between false, true and numbers and the above is certainly golfish.
regards,
Brian
Parent comment
Brian: Both # Missing the [i,j] operator and # Missing the [i,j]=v operator is false, they can be implemented with __getitem__ and __setitem__. Joins of lists are usually not done with an imported string module but with "sep".join(list) wich makes some sense if you think of it. "With this string join this list" instead of as in Ruby "with this list join with this string". if a[i] == b[j]: . cij = 0 else: . cij = 1 Could instead be written as int(not a[i] == b[j])
Replies
__getitem__ and __setitem__ get their arguments as a tuple. l[2,3] will call l.__getitem__((2,3))
What's wrong with converting a bool to int if it makes the code cleaner and easier to read?
Also, the to_s method should probably be replaced with __str__ instead, wich will be called by str(object). The reason of the usage of these functions to help functional programming, for instance: map(str, someCollection) wich i Ruby would be: someCollection.map{ |obj| obj.to_s }
Thank you for the hints. I'll try it later today.
Regarding the bool to int conversion it is my opinion that a boolean is a type with a domain of {false, true} and I do not have an implicit mapping from {false, true} to int. It is as sensible for false to map to -1 as it is to map to 0. The same holds for true. So I do not want to rely on this arbitrary mapping wich seems more like a coincidence than a resonable idea to me.
_Join the equal rights for zero movement_
Why should 0 be false even if it is a completely normal number?
regards,
Brian
Ah, I found a way to solve this in python. I can define a hash indexed by true and false. e.g.
{true: 0, false: 1}[a[i] == b[j]]
does the same as
a[i] == b[j] ? 0 : 1
Nice, that's a trick I haven't used before. I like it. :)