Giter Club home page Giter Club logo

python-parameter-talk's Introduction

This is a short set of examples to show how Python parameters work. It has a specific point of showing how kwargs work.

Showing the differences between:

  • keyword argument vs keyword parameter
  • argument defaults and behavior

named_parameters.py

Just because have names does not mean you must specify them. These can be viewed as positional parameters. Note, all parameters are position, however, some may have default values.

named_parameters_default_value.py

If a default value is given to a parameter is not a keyword argument. It is just a parameter with a default value. This means you do not need to specify a value for the parameter.

named_parameters_keyword_arguments.py

The order of keyword arguments is irrelevant. As long as positional arguments are satisfied first the ordering of the named parameters with default values does not matter.

So when using keyword arguments the position of arguments does not matter.

kwargs.py

A bit on how **kwargs work. That **kwargs is a special "gobble" up the rest of the keyword arguments.

args.py

A bit on how *args work. That *args is a special "gobble" up the rest of the positional arguments.

Default Values

Default values are not always the same. Python keeps default values assigned to parameters the same, respecting the rules of mutable data types.

Take a look at fib.py, fib_cached.py, and fib_cached_broken.py.

It's an example of why this behavior can be useful and why it works how it does work. Specifically the known_values being modified vs reassigned effects how the default parameter works. If the value is reassigned it does not effect the default parameter because the parameter is not modified. When known_values.append is used the default parameter is changed. This because the actual value is modified.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.