Search code examples
pythongoogle-colaboratory

Positional Only Parameter Syntax '/' in Google Colaboratory


Firstly, I'm using Python 3.11.0 in Colab.

When I use / to make parameters positional-only, SyntaxError is raised.

However, when I use * for keyword-only parameters, any error hasn't been raised.

Is there anyone who knows why these things happen?

# SyntaxError is raised
def foo(x, y, /):
    return x + y
  File "<ipython-input-28-57597574dc0a>", line 1
    def foo(x, y, /):
                  ^
SyntaxError: invalid syntax
# This was ok
def foo(*, x, y):
    return x + y

Solution

  • Python introduces the new function syntax in Python3.8.2 Version, Where we can introduce the / forward slash to compare the positional only parameter which comes before the / slash and parameters that comes after * is keyword only arguments. Rest of the arguments that are come between / and * can be either positional or keyword type of argument.

    Google colab version :

    enter image description here