Some disclaimer-type prelude; much of this question was taken on a challenge to "is it possible to do this", moreso than IF it's a good idea to do this in real production code - as a way to expand my understandings.
What I wanted to do is to subtype int
such that whenever the "value" of the type was accessed it printed a log statement to the console.
if you do something like dir(4)
you get a list of overrideable functions coming from int
. 4
itself is caught as being of type int
. I thought that perhaps by finding the right overrideable function that the subtype could be like:
class subInt(int):
def __new__(cls, value):
return int.__new__(cls, value)
def __someFunc__():
print('accessing')
return int.__someFunc__()
My immediate question is how to pull this off, but the bigger picture question is how does Python (Python3 if it matters) manage the tie between the symbol 4
and the int 4
behind the scenes?