Is there any difference between calling "str (my_object)" and "my_object .__ str __ ()" in Python?

3

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

Example:

from uuid import uuid4
uuid4().__str__()

The output is:

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

However, the same happens with calling str passing that same object as a parameter.

str(uuid4())

The output is:

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

I have some questions about this:

  • Is there any difference between calling method __str__ ( my_object.__str__() relative to str ( str(my_object) )?)

  • The __str__ method is used by the str function?

  • Considering the best practices I should apply to the language, which one should I use to return the object as a string?

asked by anonymous 10.09.2015 / 18:12

3 answers

3
  

Is there any difference between calling __str__ ( my_object.__str__() against calling str ( str(my_object) )?

No. According to the documentation , str() calls objeto.__str__() .

  

The __str__ method is used by the str function?

Yes.

  

Considering the good practices I should apply to the language, which one should I use to return the object as a string?

str() . It will take care of producing the appropriate formatting for the output in question.

    
10.09.2015 / 18:17
3

Answering the questions as they appear:

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

      

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

  • The __ str__ method is used by the function str?

      

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

  • Considering the good practices I should apply to the language, which one should I use to return the object as a string?

      

    By PEP conventions, by putting two underscores around the name of a method, a programmer is suggesting to others that such a method should not be used outside the scope of the implementation of the object on which it is defined. So, best practices suggest using str (OBJECT) to return a method as a string.

11.09.2015 / 05:10
1

Apart from the other two answers, which are correct, it is worth mentioning that the default implementation of __str__ - which is in the object class, from which all Python objects derive, makes a call to the __repr__ : that is, if your class does not set __str__ but set __repr__ , the output of that% is what is returned when str(seu_obj) is called. If you redefine str you can have a repr and a str distinct.

Python presents the repr form 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 itself, the output str is displayed without quotation marks, whereas repr includes the quotation marks, 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 escaped form with \xHH )

    
02.02.2017 / 01:19