An introduction to the new `/` syntax in Python 3.8.
Originally published by Sanket at https://deepsource.io
The ability to specify positional-only arguments using the
/ marker in function definitions is among the many new improvements to the Python language coming in the upcoming 3.8 release. This addition to syntax has performance benefits and enables better API design. Let's look at the motivation behind positional-only arguments and how to use it, with examples.
Keyword-only arguments have been available in Python with the
* marker, and addition of
/ marker for positional-only arguments improves the language’s consistency. With positional-or-keyword parameters, the mix of calling conventions is not always desirable. Consider these examples:
namedtuple(typenames, field_names, …)
arg2, …, etc for
If the users start using a keyword argument, the library author cannot rename the parameter because it would be a breaking change. In case of
min(), the name of the parameter provides no intrinsic value and forces the author to maintain its name forever since callers might pass arguments as a keywords. This problem is solved by positional-only arguments. In addition, the parsing and handling of positional-only arguments is faster.
To specify arguments as positional-only, a / marker should be added after all those arguments in the function definition. Take this example:
def pow(x, y, /, mod=None): r = x ** y if mod is not None: r %= mod return r
The following would apply:
/(in this case,
y) can only be passed positionally.
modcan be passed positionally or with a keyword.
>>> pow(2, 10) # valid
>>> pow(2, 10, 17) # valid
>>> pow(2, 10, mod=17) # valid
>>> pow(x=2, y=10) # invalid, will raise a TypeError
>>> pow(2, y=10) # invalid, will raise a TypeError
def table_format(items, separator=',', /, prettify=False):
requiredpositional-only parameters (in this case,
A function definition, with all flavors of argument-passing variants, would look like this:
def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
----------- ---------- ----------
| | |
| Positional or keyword |
| - Keyword only
-- Positional only
Positional-only arguments were proposed in PEP 570, and it’s worth talking a look at the doc to understand the feature in more detail.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?
After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...
What is neuron analysis of a machine? Learn machine learning by designing Robotics algorithm. Click here for best machine learning course models with AI
Python For Machine Learning | Machine Learning With Python, you will be working on an end-to-end case study to understand different stages in the Machine Learning (ML) life cycle. This will deal with 'data manipulation' with pandas and 'data visualization' with seaborn. After this an ML model will be built on the dataset to get predictions. You will learn about the basics of scikit-learn library to implement the machine learning algorithm.
Using Scikit-Learn for Machine Learning Application Development in Python - Python is arguably the best programming language for machine learning. However, many aspiring machine learning developers don’t know where to start...