Is this a good idea or a waste of time?
Antoon Pardon
apardon at forel.vub.ac.be
Tue Aug 29 03:52:44 EDT 2006
On 2006-08-29, Jonathan Gardner <jgardner at jonathangardner.net> wrote:
>
> Simon Forman wrote:
>>
>> If you have a reason to restrict your code to using only ints (perhaps
>> you're packing them into an array of some sort, or passing them to a C
>> extension module) then yes, of course it's appropriate.
>
> I politely disagree. Rather than an interface that demands an actual
> int, demand something that can be typecast as an int.
>
> For instance:
>
> def needsInt(i):
> i = int(i)
> ... pass i to an internal c function that requires an int ...
> # Or better yet, write your internal c function to take any Python
> object and cast it into an int.
>
> If you absolutely need a particular type of thing, then cast it into
> that thing.
The logical conclusion of this decision would be that one should write
sequence classes as follows:
class Seq(object):
...
def __getitem__(self, index):
index = int(index)
...
def __setitem__(self, index, value):
index = int(index)
...
I don't know about you but I personally think this is overkill.
I would also prefer seq[2.5] to raise an exception instead
of returning seq[2]
--
Antoon Pardon
More information about the Python-list
mailing list