Python Forum
python 3.8 and positional arguments - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python 3.8 and positional arguments (/thread-19018.html)



python 3.8 and positional arguments - vojo - Jun-10-2019

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


RE: python 3.8 and positional arguments - Larz60+ - Jun-10-2019

Python-forum.io is not the the official site of python

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

Thank you.


RE: python 3.8 and positional arguments - DeaD_EyE - Jun-10-2019

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.


RE: python 3.8 and positional arguments - metulburr - Jun-10-2019

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/


RE: python 3.8 and positional arguments - DeaD_EyE - Jun-10-2019

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.