Python Forum

Full Version: Interesting new features in python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I love that PEP 498: Formatted string literals are coming in python 3.6 !

>>> 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'."
Which are your favorite new python features since 2.x?
I also like the string formatting, but don't use them because if someone is running python older than 3.6 they won't work
The ordering of dictionaries is a welcome addition, but not sure what it does to performance.
The natural hashing algorithm does not by nature order by key. The only way that I know how to do this (and I have used it in my C code),
is to keep a separate index that is sorted with a cross reference to the hashed entry.
Things I like since Python 3.x and special since 3.6:

  1. New String formatting
  2. preserved order in dicts; an implementation detail, but if we rely on it, they'll may be guarantee it in 3.7.. maybe
  3. smaller size of dicts and the speed optimization
  4. generalized unpacking:
    first, *middle, last = iterable
    def func(*args, **kwargs):
        return args, kwargs
    
    args1 = (1,2,3)
    args2 = (4,5,6)
    kwargs1 = {'foo': 'bar'}
    kwargs2 = {'boo': 'baz'}
    output = func(*args1, *args2, **kwargs1, **kwargs2)
    print(output)
    Output:
    ((1, 2, 3, 4, 5, 6), {'foo': 'bar', 'boo': 'baz'})
  5. preserved order of kwargs, they guarantee it since Python 3.6
  6. modules: ipaddress, pathlib, secrets, hashlib.pbkdf2,
  7. async stuff: async with, async for, async generator support, not yet used it, but it sounds cool
  8. more stuff i can't remind now
1. so how long must a feature be in python for you to start using it? 2, or what percentage of your user base needs to be using a python version that include a feature for you to start using it?

for me: 1. a few years unless i have been asking for the feature and then fewer years. 2. 57-85%
It depends on the feature. If it is something useful, I will use it right away. Since I am my own user base, the only stumbling block is the OS. In Windows I'm using Python v 3.6.1 (soon to upgrade to 3.6.2?) while two of the Linux machines and the RaspberryPi are running 3.4 (though I did manually install 3.6.1 on the Pi) so I sort of have to keep an eye on that if I write something cross platform. Also, to be honest, I probably have never used and maybe never will about 50% of the core library's. I suppose I really should broaden my horizons LOL
For your own private projects without given dependencies to other modules, you should take the newest.

There is a simple solution for it: pyenv and pyenv-installer.
I'm using for my projects at work currently the newest Python version because I do also build and install the hardware.
Currently I have a bigger Project, which have to be ported from Python 2.7 to 3.x.
(Jun-29-2017, 01:16 AM)Larz60+ Wrote: [ -> ]I also like the string formatting, but don't use them because if someone is running python older than 3.6 they won't work
Ditto
(Jul-05-2017, 06:13 AM)Skaperen Wrote: [ -> ]1. so how long must a feature be in python for you to start using it? 2, or what percentage of your user base needs to be using a python version that include a feature for you to start using it?
I wouldnt use them because there is still a large base on 2.x As well as anyone using 3.0-3.5. I like to write my code where 2.x or 3.x (any version) can run it. I will probably wait to use them till after 2.x is dead in a couple years. By then all would have moved to 3.x and most moved to a version after 3.6.

I am just now favoring 3.x since 2011, so i suspect it will take awhile before i use new features.
My rule is simple, and rather cowardly. I simply wait for others to start using it.
What version am I using? That's what I use.
Am I distributing something for other people? Then I'll make minor modifications so it works with a wider range of versions.

Normally, I'm not going to waste my time to accommodate a version of python I'm never going to use. ESPECIALLY if I'm not even going to share what I'm writing. I'd rather just play more Stardew Valley.