0

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?

DoubleDouble
  • 328
  • 2
  • 12
  • The symbol only exists when perfoming input or output of values, it's processed by the parser, just like the `[]` around lists. Once the program has been parsed, everything is just raw data, there are no symbols. – Barmar Oct 27 '22 at 22:35
  • I don't think there are any customizable methods used when getting the raw value of an object. – Barmar Oct 27 '22 at 22:37

0 Answers0