On Feb 16, 2020, at 05:24, ananthakrishnan15.2001@gmail.com wrote:
This module should contain operations that can be performed on binary numbers.
I think you have two problems here. The first is that you’re confusing integer values with integer literal syntax. It’s not just that `0b101` and `5` are the exact same number, 5, it’s also that all of your operations already make sense for that number 5, and almost all of them are already implemented as operators or methods in Python. So the main part of your question doesn’t even make sense. But there is an additional problem: you want integers fixed to a specific bit length (and signedness). And not even necessarily the native machine lengths 32 and 64; you want to be able to do 3-bit arithmetic on 3-bit integers. For this, what you want is probably not a module with a bunch of functions on int, but a new class. I’ll call it BinaryInt, and construct it as BinaryInt(n: Integer, *, bits, signed=False), but I think a name that was about being fixed-length instead of about being binary might be less confusing, and of course you can decide on any constructor form you want. (You may also want to be able to take strings, a la decimal.Decimal.) Whether it’s a subclass of int is up to you. The former is more convenient to implement, but subtyping is more important here than implementation. Do you want to be able to pass a BinaryInt(0b101, bits=4) to a function that wants an int and have it act as the integer 3, or to get a TypeError when you try it? (And, if the former, should mypy statically understand that is-a relationship?) In fact, there are actually multiple questions here: first, is-a int, is-not-a int but has an __int__ method, or not even that; next, if you’re not an int subtype, you have to decide whether to fit into the numeric tower as an Integer (by subclassing or registering with the ABC); finally, how you want to allow mixing types in operators (whether 1+BinaryInt(0b101, bits=4) should be 6, BinaryInt(0b110, bits=4), or TypeError). You also have to define what happens with overflow and underflow. When you add 4-bit unsigned 1110 and 1111, do you get 1101 (modulo, like C), 1111 (saturating), or OverflowError? Is it different for signed? And what happens when you add two numbers of different bit lengths? You can choose whatever str makes sense for your binary ints, but its repr should probably look like the constructor call, probably “normalized”, so `repr(BinaryInt(5, bits=4))` gives you `BinaryInt(0b0101, bits=4)` and `repr(BinaryInt(-2, bits=4))` gives you `BinaryInt(0b1110, bits=4))` and `repr(BinaryInt(-2, bits=4, signed=True))` gives you `BinaryInt(-0b010, bits=4, signed=True)`. Without deciding all of these things, you don’t really have a proposal. But once you do, implementing all the dunder methods is easy, if a bit tedious. (You may want to look at the implementation of Fraction for some useful helper tricks, especially if you want to act like an int when dealing with other types, or even just allow adding different bit lengths.) The handful of things that aren’t builtin operators or methods of int, like Gray coding, you can decide whether to makes these methods on your type or functions. (I think methods make more sense. After all, how can you do gray(8) without specifying it’s supposed to be the 4-bit Gray code? But BinaryInt(8, bits=4, unsigned=True).gray() has no such problem.) I doubt this type would be accepted into the stdlib, but even if it isn’t, once you build it, you have it, and you can share it on PyPI for others who want it. Of course first you should search PyPI and make sure it doesn’t already exist. There are definitely modules for bitstrings, fixed-power-of-2-length ints, and all kinds of other related things, but I don’t know if there’s one for fixed-arbitrary-length ints.