I've encountered a question on a course in oop in python regarding bin(). The author wanted to modify it in such a way that it would return the binary representation of a number in a different format.
For example, bin(30), instead of '0b11110' would output '00011110'. This can be done, for instance, by a workaround
bin(30)[2:].zfill(8)
According to the documentary and this stack question, this can be done also by
f'{30:08b}'
Is there a smart way to define a class to do just that? Note, a beginner here. Here's my attempt:
class my_bin(object):
def __init__(self,value):
self.binary = format(value, '08b')
def __repr__(self):
return str(self.binary)