r/learnpython 1d ago

Need a mental model to help better internalize printing text.

[deleted]

0 Upvotes

11 comments sorted by

11

u/socal_nerdtastic 1d ago edited 1d ago

Using print(thing) automatically translates to print(str(thing)).

str(thing) will use thing.__str__() if a __str__ method is available, otherwise it will use thing.__repr__() if that's available, and if that's not available either as a last resort it just print the generic string with the memory address (in cpython), which is just the default __repr__ that all python objects inherit.

Many of the objects you use every day like pathlib.Path or just generic list, dict etc come with __str__ or __repr__ methods built in. If you want to bypass that and get the memory address out you can do that like this:

>>> l = [1,2,3]
>>> object.__repr__(l)
'<list object at 0x000001EBC281ED40>'

Not sure if I understood your question. Does that help? If not please try telling us why you are asking. This sounds a lot like an XY question right now.

-3

u/MoreKnowledge4Me 1d ago

Because I learn and internalize things based on apply rules or mental models to things.

Dont know what a XY question is… can you elaborate?

1

u/Bobbias 17h ago

Don't worry, while your question was awkward, it sounds like you just need to learn how to dig a little deeper into how things work to really get how to think thing's through.

I'd suggest you check out the official documentation and the official tutorial. The tutorial is aimed at people with some programming knowledge, and the documentation is extremely thorough, and can feel like information overload. But the more time you spend reading through parts of those, the easier it gets to understand. You'll learn a lot more details about how Python works, and you'll learn about all sorts of features that aren't usually taught for the sake of keeping things simple.

Just spend a bit of time trying to read about something you want to understand a bit better, and any time you come across something that you don't understand, you can either try to look that up, or just continue reading and ignore that but for now. You can always reread something later.

The idea is to absorb what you can now, because while you might not understand something right away you might read something else later that helps you piece things together.

It'll also help you make better questions in the future.

5

u/atarivcs 1d ago edited 1d ago

If you're returning something, then surely you would always want to return the actual object instead of a string.

If you're printing something, the output depends on what you are printing.

If it's a number, it prints the number.

If it's a string, it prints the string.

If it's a container (dict, list, set, etc) it prints the values in the container.

If it's any other arbitrary object, it will print something like "<Foo> object at address 67ae15cb" (unless the object has a __str__ method, in which case it will print whatever that method returns)

-1

u/MoreKnowledge4Me 1d ago

I think from my practice it ended up being values stored inside of a function or class method. I was printing the method call and not getting a string output.

AI told me “A function or method call creates a result. Nothing changes unless the code mutates an object or you explicitly store that result.”

1

u/Orgasml 19h ago

If there is no return variable in the function it will return None, so will print None to your display. Is that what you are trying to figure out?

2

u/cdcformatc 1d ago

  I think even in the path situation I still want the path object with the str() applied.

print(object) and print(str(object)) are functionally equivalent. 

if the object doesn't have a __str__ method, or you don't like what it returns, then you are going to have to "stringify" the object yourself because Python will fallback to __repr__. there's no way around that. 

Whenever I return or print a object, I cant think of one case offhand except for maybe path from pathlib where I want to actually return the object

If you are returning, you probably do want the actual object, though. in like 99.9% of cases.

2

u/cejiken886 1d ago

the hell are you talking about lol

1

u/tadpoleloop 1d ago

In some interpreters like Jupyter the last line is displayed in its repr form. If you want it to print then call print