Python 3D Academyবাংলা

*args, **kwargs & Scope in Python

Chapter 15: *args, **kwargs & Scope

5 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Local and global names

    Names made outside any function are global, names made inside a function call are local to that call, and Python looks for a name in the local frame first, then the global one.

  2. The global keyword (and why to avoid it)

    Assigning to a name inside a function makes it local, unless you first write global name, which makes the function use the global name instead.

  3. Any number of arguments: *args

    A parameter like nums collects *all the extra positional arguments** into a tuple, so a function can take any number of them, even none.

  4. Named extras: **kwargs

    A parameter like extra collects the **extra keyword arguments, the named ones with no parameter of their own, into a dictionary.

  5. Keyword-only and positional-only parameters

    A bare * in a function's parameters means every parameter after it must be given by name, and a / means every parameter before it must be given by position.