inheriting from long on Python2 or int on Python3
duncan smith
duncan at invalid.invalid
Sat Mar 21 20:25:22 EDT 2020
On 21/03/2020 21:55, Skip Montanaro wrote:
>> import sys
>>
>> class C_hash(int if sys.version_info.major >= 3 else long):
> ...
>
> There's nothing incorrect with your solution. Chris offered another. I
> was going to suggest you consider running your code through 2to3 to
> see what it does. I created a C_hash class similar to yours:
>
> class C_hash(long):
> def __new__(cls, bits, m, N=1):
> obj = super(C_hash, cls).__new__(cls, bits)
> obj._m = m
> obj._N = N
> return obj
>
> def meth(self):
> pass
>
> When I ran it through 2to3, it didn't make any transformations, which
> I thought odd. Still, assuming I did something wrong, you might poke
> around with it a bit to see if you can get it to do the work for you.
>
> Skip
>
According to the docs (https://docs.python.org/2/library/2to3.html) it
should have renamed long to int. I also tried something closer to
Chris's suggestion,
try:
long
except NameError:
long = int
class C_hash(long):
# etc.
but (tentatively) opted for something that avoided creating new names at
the module level. If it doesn't look too terrible I might stick with it
for now (although I am quite tempted to go back to the above). Thanks
Skip and Chris.
Duncan
More information about the Python-list
mailing list