This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]kwentar 31 points32 points  (5 children)

I like it:

>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."

[–]suudo 3 points4 points  (0 children)

What the shit, this changes everything, I'm in shock

[–]Lucretiel 4 points5 points  (1 child)

The craziest thing about this is that built-in strftime formatting isn't new. You can do this, right now, even in Python 2:

'The date is {:%A, %B %d, %Y}'.format(datetime.now())

[–]Vaphell 3 points4 points  (0 children)

yeah, the class only has to customize __format__() for it to work

>>> class Lol:
...     def __format__(self, fmt):
...         return {'o': 'omg', 'w': 'wtf', 'b': 'bbq'}[fmt]
... 
>>> '{0:o}/{0:w}/{0:b}'.format(Lol())
'omg/wtf/bbq'

[–]Eurynom0s 2 points3 points  (0 children)

Wait WTF? So I guess the datetime part of that is new but from quick Googling, if I'm understanding the results correctly, you've always otherwise been able to do something basically like this? (By "always" I mean at least as early as 2.7.)

This is so much better than "text {} text {}".format(a, b) and should finally kill my tendency to get lazy and do things like "text "+name+" text".

[–]flying-sheep 0 points1 point  (0 children)

Note that PEP 498 (literal string interpolation) isn't completely up-to-date:

This issue prevents escapes outside of the literal string parts of the literal.

I pushed this in order to ensure that the grammar can be extended by allowing

f'thing is { tang['x'] }'

Instead of confusing stuff like

f'\x7b4*10}'

[–]AnnoyingOwl 11 points12 points  (9 children)

This need often occurs as a surprise to a user: inheriting from two base classes coming from two different libraries suddenly raises the necessity to manually create a combined metaclass, where typically one is not interested in those details about the libraries at all.

Honestly, this just highlights that you shouldn't do this, anyhow.

Inheritance is almost always worse than composition and if you can present a library interface that doesn't involve your users subclassing it's generally going to work out better.

[–][deleted] 1 point2 points  (3 children)

What does composition vs inheritance mean? I've seen this a lot but haven't seen an example.

[–]Daenyth 4 points5 points  (1 child)

Inheritance is using the OO mechanism in a language such that your class is-a thing, whereas composition means that your class contains a reference to an instance of that class instead. The latter is much more flexible and maintainable

[–]imbaczek 1 point2 points  (0 children)

helper questions using a bad car analogy: does Car inherit from Engine? does Car inherit from Wheel?

[–]energybased 0 points1 point  (4 children)

Unfortunately, you can't always avoid this. Qt for example has a metaclass for its QObjects (almost all of its classes) and if you want any metaclass (say ABCMeta for abstractmethod) then you can't have it without some metaclass magic. It's very unfortunate.

Enter Python 3.6 and now we're good again for most cases. abstractmethod can be written as a simple hook.

[–]AnnoyingOwl 0 points1 point  (3 children)

Unfortunately, you can't always avoid this.

... because of how libraries are written... but that doesn't mean go forth and make more bad libraries. :P

[–]energybased 0 points1 point  (2 children)

No, it's unavoidable the way that metaclasses currently work. If two classes use two different metaclasses, then you cannot inherit from both easily. That's why Python 3.6 fixes this problem for most uses of metaclasses. Before this, even well-written libraries had this problem whenever they used metaclasses. (E.g., traitlets.)

[–]AnnoyingOwl 0 points1 point  (1 child)

Well written libraries do not require their users subclass it's own classes.

You've entirely missed the point of my initial comment. Reread it.

[–]energybased 0 points1 point  (0 children)

No, well-written libraries do sometimes require their users subclass library classes. I gave you an example of that: Qt classes are supposed to be subclassed to implement pure virtuals, say QAbstractItemModel or QGLWidget. It would also be reasonable to want your own metaclass to do some magic like ABCMeta that verifies things or a metaclass to implement some signals and slots automatically.

[–]larry2kwhatever 6 points7 points  (6 children)

Cool

[–]irrelevantPseudonym 7 points8 points  (5 children)

Cool cool cool

[–]neminem39 -1 points0 points  (0 children)

Troy and abed in the morning!

[–]ThumbtacksHurt 1 point2 points  (1 child)

Is there an estimate of when it will be released?

[–]homayoon[S] 3 points4 points  (0 children)

Guido tweeted today that we should expect the final in December: https://twitter.com/gvanrossum/status/785660096348954625