Python Forum
python 3.8 and positional arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3.8 and positional arguments
#1
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
Reply
#2
Python-forum.io is not the the official site of python

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

Thank you.
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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/
Recommended Tutorials:
Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 399 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  How do I handle escape character in parameter arguments in Python? JKR 6 1,159 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
Question log.exception() without arguments in old Python versions? cthart 5 1,164 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,947 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: missing 3 required positional arguments: wardancer84 9 10,905 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,217 May-15-2021, 04:15 PM
Last Post: Anldra12
  How to reuse positional arguments entered in terminal. rcmanu95 1 1,877 Jul-04-2020, 01:00 AM
Last Post: bowlofred
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 11,712 May-31-2020, 03:13 PM
Last Post: deanhystad
  TypeError: add() missing 2 required positional arguments NectDz 5 13,108 May-28-2020, 02:54 PM
Last Post: BitPythoner
  add() takes 2 positional arguments but 3 were given Man_from_India 3 5,792 Feb-10-2020, 05:08 PM
Last Post: Man_from_India

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020