Is there a difference between "str(my_object)" and "my_object. __str__()" in Python?

Asked

Viewed 176 times

3

I observed that, in Python, when we have an object with the method __str__, it is responsible for returning a string representing the object - or something like that.

Example:

from uuid import uuid4
uuid4().__str__()

The exit is:

36cdc126-9d4d-43f9-9ede-bef8e15b834c

However, the same is true with the call of str passing this same object as parameter.

str(uuid4())

The exit is:

 36cdc126-9d4d-43f9-9ede-bef8e15b834c

I have some questions about that:

  • Is there any difference between the method call __str__ (my_object.__str__()in relation to the finding of str (str(my_object))?

  • The method __str__ is used by the function str?

  • Taking into account the good practices I should apply to the language, which of the two ways I should use to return the object as a string?

3 answers

3

Answering questions such as those posed:

  • Is there any difference between the method call str (my_object.str()for str (str(my_object))?

    In most cases, no. However, some python implementations may perform differently between using the generic str(OBJECT) and OBJECT function. __ str__(), due to the implicit use of optimizations. When in doubt, use the generic function of the language (str(OBJECT)).

  • The__str__ method is used by the str function?

    Yes. Part of the basic Python language design prevents the creation of inaccessible methods within modules. However, by the PEP conventions, methods that should not be used by other modules (methods in JAVA called "protected") have its name preceded and raised by two underscores ( __ NAME__ ).

  • Taking into account the good practices I should apply to the language, which of the two ways I should use to return the object as a string?

    According to the PEP conventions, by placing two underscores around the name of a method, one programmer is suggesting to others that such a method should not be used outside the scope of the implementation of the object in which it is defined. Thus, best practices suggest the use of str(OBJECT) to return a method as a string.

3


Is there any difference between the method call __str__ (my_object.__str__() regarding the call for str (str(my_object))?

Not. According to the documentation, str() flame objeto.__str__().

The method __str__ is used by the function str?

Yes.

Taking into account the good practices I should apply to the language, which of the two ways I should use to return the object as a string?

str(). It is in charge of producing the proper formatting for the output in question.

  • 3

    You use the # to represent my questions. You don’t think the > would be better?

  • Whatever. If you want to edit, feel free.

  • 2

    @Ciganomorrisonmendez I think you should review the formatting you do in your responses, the use of # makes sense in a longer response with subtitles, the current way creates visual pollution making it difficult to read. Think that your answer is like a document to be delivered to a client or a college professor, I’m not saying to follow ABNT standards, but this your current pattern of using # not good.

  • 1

    @Orion Honestly, I don’t see this visual pollution you’re talking about, but I can replace the signals, no problem.

  • Pow dude, don’t you see the pollution in this post? It’s polluted like hell!!

1

Apart from the other two answers, which are correct, it is worth mentioning that the standard implementation of __str__ - classy object, from which all Python objects derive, make a call to the function __repr__: that is, if your class does not define __str__ but define __repr__, the output of the latter is what is returned when called str(seu_obj). If you redefine yourself str may have a repr and a str distinguished.

Python presents the form repr in addition to str as an object view that is most convenient in interactive environments - such as the Python terminal or the debug prompt. For example, for strings themselves, the output str is displayed without quotation marks, while the repr includes quotes, and shows some Unicode characters in the escape form '\uHHHH' instead of the characters themselves. (In Python 2.x, all non-ASCII characters were displayed in an escaped form with \xHH)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.