What's new in Python 3.8?? | extrovert.dev -->

What's new in Python 3.8??

What's new in Python 3.8??
Thursday, October 17, 2019

Python updates its version very quickly and gifts us with new features.We must thank the developers who made these great features for us. Today we will see Python 3.8, a new release of Python.


Assignment Expressions:
       This expression assigns values to the variables as a part of an expression without the need for initialization upfront. And this is known as "walrus operator" due to its resemblance to the eyes and tusks of walrus. This walrus operator is used with while-loops that compute a value to test loop termination and needing that same value again in the loop. One important point to be noted here is we need to ensure that the assignment of a variable is parenthesized.

         Lets us see an example,
In previous versions of Python, code is written as follows
          while chunk:
                               x=file.read();
Now we using walrus operator:
          while chunk := file.read();
Usage of walrus operator helps in optimizing the compilation time and also we can save the variable memory.

EXCEPTIONS:


  • Unexpressions are prohibited at the top level of an expression statement.
  Example,
y=f(x)
#invalid
  (y:=f(x))
#valid, but not recommended.



  • Unparenthesized expressions are prohibited for the value of a keyword argument in a call.

 Example,
foo(x=y:=f(x)) 
#invalid
foo(x=(y:=f(x))) 
 #valid but confusing...
  •  Unparenthesized assignment expressions are prohibited as annotations for arguments, return values and assignments.

Example,
def abb(answer:p:=42=5):
 #invalid
def abb(answer:(p:=42)=5) :

#valid


Positional-only parameter (/):

Position only arguments will help you to specify an argument positional-only in its function. For example, in built-in function pow() we can't use power(x=4)  because it only supports positional arguments.
def v(x):      return x
>>v(8)8>>v(x=8)8

Rewriting the code with /


def v(x , /):       return x
>>v(8)8>>v(x=8)Traceback (most recent call last):  File "", line 1, in
TypeError: v() got some positional-only arguments passed as           keyword arguments: 'x

Position-only parameters give flexibility and control to the library authors to safely develop an API.

= now supported in f-string:

Now = is supported in f string. Probably most people will be waiting for this.
>>> python = 3.8
>>> f"python={python}"'python=3.8'

Yet there are many optimizations made to python 3.8
yets=9'

0 Response to What's new in Python 3.8??

Comments are personally moderated by our team. Promotions are not encouraged.

Post a Comment