How is the __format__ method supposed to be used for int?

I saw that there was a __format__ method but help ( int.__format__ ) does not provide any help.

I also know that you should not call __method__ directly. When is this method called? What is his argument?

+4
source share
2 answers

It is used for the new Py3k string formatting scheme.

You can find more information here:

http://docs.python.org/whatsnew/2.6.html#pep-3101-advanced-string-formatting

You are right that it is not called directly. It is called str.format or the new format built-in.

+3
source

It is used when you pass an integer to the format () function. Details elude me, because I cannot get him to tell me what the argument is. (Edit: see Link with a lost theory)

Oh, and it only works when the only argument is an integer. If you pass a tuple for formatting, the tuple .__ format__ and int .__ str__ function are called or something.

 '{0}'.format(4) str(4.__format__(format_spec='')) 
+2
source

Source: https://habr.com/ru/post/1299986/


All Articles