0
[SOLVED]Why's it that print(classname) doesn't output the class' address but print(functionname) outputs the function's address?
with class, the output is like: <class '__main__. classname' > with function, it's: <function functionname at (insert random_14digit_hexadecimal_value) > I was bored, sorry for the names I used in the code, I don't think it's needed anyway... Edit: also what is __main__. Classname as an entity? It's not an ID, not an address, and is a method used on dunder function object? - - - - >>>> lol I probably misused terms, I cannot even tell, which is why I am asking this, too. Thank you all. https://code.sololearn.com/c747nnrXDhRe/?ref=app
8 Answers
+ 2
As to the original question, why class representations look different from function representations that's a design choice, and I can only speculate about the reasons. If you take a look at the code below you see that there is a number of different representations.
They all include the type and the name.
The representations of custom functions and of instances of classes include the memory address.
The representations of imported modules include the directory address.
The representations of custom classes include "__main__" as part of the name.
The representations of classes from imported modules include the name of the module (including substructure) as part of the name.
"__main__" refers to the main module, i.e. the file that is being executed.
https://code.sololearn.com/c9xuOo8p0KaW/?ref=app
+ 3
because print() requires a string and this string is contained at a magic method called __repr__() which returns the `representation` of an certain object, but in abstenceof this method by default is taken type(object).
So add it as for example:
def __repr__(self):
return 'MyName'
and at the line 10 just add () behind `ugh` to call it!
+ 2
Ervis Meta - SoloHelper actually when you use print() the __str__() method is called. Only when this is not defined Python falls back to the __repr__() method. If neither is defined the object's memory address is used.
The difference between __str__() and __repr__() is that the former is supposed to be human readable while the latter is supposed to be computer readable.
https://code.sololearn.com/cryThVlgOfEo/?ref=app
+ 2
I assume that the reason for the differences is that the developers chose the information they considered to be most relevant. For modules that is the directory address, for classes where they are defined (either the main file or an imported module), for functions the memory address, etc.
+ 1
@Simon Sauter I forgot to mention that, thnx for spotting it out !
+ 1
Simon Sauter Thank you soo much. You have a very clear way of explaining things. All the best!
(I forget to scroll from the right at times and undo my votes or vote without realizing, sorry for the clumsiness, you probably got an extra unnecessary notification)
0
Ervis Meta - SoloHelper Hello, thank you for the response. I am confused, though:
print(3*3) can be outputted without the argument being a string?
So you're saying that the built in print function definition by default is to pass its argument to __repr__() and have the output converted to a string?
Then why would this magic method be absent, or when? (I called print for both after all)
Also aren't the function and the class both objects each with an address?
Why can I see the function object's address in the output as well as its type but not the class object's? Type is default, address isn't?
0
Thank you both for explaining this. Because I'm a noob, I cannot tell if it is knowledge that I'm lacking in, or if I'm failing to understand something that I should be able to at my current level.
With built in stuff that's default, especially, this is often the situation. So this is how these replies have helped me. I really want to make sure I understand the output I get and why I get it.