Python Forum

Full Version: python 3.8 and positional arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
saw the plans for 3.8. Trying to understand how these 2 functions would be handled

def pos_key (pos only * pos or key / key only):
blah blah

def whatever( my_cool_var = 4 * 5 / 2):
blah blah

second not allowed (maybe never has been)?
Huh
Python-forum.io is not the the official site of python

Please post your question here: https://www.python.org/community/lists/

Thank you.
Haven't tried it, but I guess the second example does a multilication and then a division. Have never seen this in real world applications. Those constants should not be defined in the function signature.
linking
https://docs.python.org/3.8/whatsnew/3.8.html

I dont think that is what they mean when they are adding / (the second example)
https://www.python.org/dev/peps/pep-0570/
Explained with code using your whatever function:

In [6]: def whatever(my_cool_var=4 * 5 / 2): 
   ...:     print(my_cool_var) 
   ...:                                                                                          

In [7]: whatever()                                                                               
10.0

In [8]: whatever(2)                                                                              
2

In [9]: whatever(my_cool_var=2)                                                                  
2

In [10]: def whatever(my_cool_var=4 * 5 / 2, /): 
    ...:     print(my_cool_var) 
    ...:                                                                                         

In [11]: whatever(my_cool_var=2)                                                                 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-65f31df15028> in <module>
----> 1 whatever(my_cool_var=2)

TypeError: whatever() got some positional-only arguments passed as keyword arguments: 'my_cool_var'

In [12]: whatever(2)                                                                             
2

In [13]: whatever()                                                                              
10.0
This is interesting for library developers who want to enforce the use of positional arguments, followed by keyword-arguments.
Just read the PEP.