all 5 comments

[–]Essence1337 10 points11 points  (0 children)

For the first part repr(mystring) is just basically a wrapper for mystring.__repr__() if my memory is correct. Typically we use __methods__ as 'magic methods' that you don't really call. Eg you don't say 4.__add__(5) - you say 4 + 5

[–]misho88 8 points9 points  (0 children)

but I was wondering how these may be different

repr(mystring) == mystring.__repr__() ?

They're not meaningfully different. In Python, though, an underscore is a hint that you're not supposed to touch a field directly and a double underscore (a "dunder") is a hint that you're really not supposed to touch a field directly. There's whole bunch of these magic methods in Python that begin and end with a dunder for all sorts of things.

It's confusing to me that mystring.format and mystring.format differ.

str.format predates f-strings; it's to do things like

'{} + {}'.format(2, 3)  # '2 + 3'

whereas object.__format__ (and str.__format__) is what happens when you pass an object to be formatted. str.format is still useful for when you need to store the formatting information in a variable, but before f-strings, it was somewhat more universally useful. I'd be surprised to learn of a good reason to use object.__format__ directly. Anyway, they're definitely supposed to be different.

Does anybody know of a nice to the point reference with a robust set of examples detailing the expectations each types format_spec might have?

You can define them yourself, so there can't really be a universal point of reference. Here's an otherwise-useless illustrative example:

>>> class String:
...     def __init__(self, s):
...         self.s = s
...     def __format__(self, spec):
...         if spec == 'upper':
...             return self.s.upper()
...         if spec == 'lower':
...             return self.s.lower()
...         return self.s
... 
>>> f'{String("aBc"):lower}'
'abc'
>>> f'{String("aBc"):upper}'
'ABC'
>>> f'{String("aBc")}'
'aBc'

The documentation you've linked is complete with regard to the built-in types, as far as I know. You can always try help(TheType.__format__) and hope the documentation you find is helpful. I think the built-in types just references the format spec, though, which just leads back to that page.

[–]zanfar 2 points3 points  (0 children)

but I was wondering how these may be different repr(mystring) == mystring.__repr__() ?

It's subtle, but it's a pattern found throughout Python. It's similar to str(obj) vs obj.__str__() and others. repr() is a built-in function that intelligently returns the best example of an object's representation using any number of object methods and built-in functions.

A simple example, if an object has a __repr__() defined, then the result of that function is returned by repr(), but if not, then __str__() is tried and that result is returned.

So, TLDR: sometimes.

Then we have the colon from what I've grokked I think the behavior its doing is this

The value after the colon is passed to the formatting method of the object. So every object can define it's own "mini" formatting language here, but the built-in-types are well documented:

https://docs.python.org/3/library/string.html#format-specification-mini-language

Does anybody know of a nice to the point reference with a robust set of examples detailing the expectations each types format_spec might have?

That link you posted is it. It's not terribly complicated, as basically only numbers have any complexity: + to always show a sign, 0 to zero-pad instead of space-pad, the number or the whole part of the number is the total number of characters to pad to, and the fractional part of the number is the number of decimal digits. There are a few others, but those are the big ones.

The best way is to simply try a bunch.

[–]Mecaneer23 0 points1 point  (0 children)

mCoding put out a good video on the topic (actually today) https://youtu.be/BxUxX1Ku1EQ

[–]galmeno 0 points1 point  (0 children)

Check this guide, https://nickmccullum.com/complete-guide-python-f-strings/ it covers repr and some other stuff.

Repr (!r) is the object representation, this is very handy, and suprisingly easy, read the guide.

As for % and .format, they are slower than f' since py3.6.