The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 48 Python Function Methods and Interfaces - Positional Parameters, Default Parameters, Variable Parameters, and Keyword Parameters
Abstracts:
In Python, functions can take many different types of arguments, including positional, default, variable, and keyword arguments, and understanding these types of arguments is important for writing flexible and maintainable code.
Link to original article:
FreakStudio's Blog
Past Recommendations:
You're learning embedded and you don't know how to be object oriented?
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 00 Introduction to Object-Oriented Design Methods
The network's most suitable for the introduction of object-oriented programming tutorials: 01 Basic Concepts of Object-Oriented Programming
The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 02 Python Implementations of Classes and Objects - Creating Classes with Python
The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 03 Python Implementations of Classes and Objects - Adding Attributes to Custom Classes
The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 04 Python Implementation of Classes and Objects - Adding Methods to Custom Classes
The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 05 Python Implementation of Classes and Objects - PyCharm Code Tags
The best object-oriented programming tutorials on the net for getting started: 06 Python implementation of classes and objects - data encapsulation of custom classes
The best object-oriented programming tutorial on the net for getting started: 07 Python implementation of classes and objects - type annotations
The best object-oriented programming tutorials on the net for getting started: 08 Python implementations of classes and objects - @property decorator
The best object-oriented programming tutorials on the net for getting started: 09 Python implementation of classes and objects - the relationship between classes
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 10 Python Implementations of Classes and Objects - Class Inheritance and Richter's Replacement Principle
The best object-oriented programming tutorials on the net for getting started: 11 Python implementation of classes and objects - subclasses call parent class methods
The network's most suitable for the introduction of object-oriented programming tutorials: 12 classes and objects of the Python implementation - Python using the logging module to output the program running logs
The network's most suitable for the introduction of object-oriented programming tutorials: 13 classes and objects of the Python implementation - visual reading code artifacts Sourcetrail's installation use
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 14 Python Implementations of Classes and Objects - Static Methods and Class Methods for Classes
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 15 Python Implementations of Classes and Objects - __slots__ Magic Methods
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 16 Python Implementations of Classes and Objects - Polymorphism, Method Overriding, and the Principle of Open-Close
The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 17 Python Implementations of Classes and Objects - Duck Types and "file-like objects"
The network's most suitable for the introduction of object-oriented programming tutorials: 18 classes and objects Python implementation - multiple inheritance and PyQtGraph serial data plotting graphs
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 19 Python Implementations of Classes and Objects - Using PyCharm to Automatically Generate File Annotations and Function Annotations
The best object-oriented programming tutorials on the web for getting started: 20 Python implementation of classes and objects - Combinatorial relationship implementation and CSV file saving
The best introductory object-oriented programming tutorials on the net: 21 Python implementation of classes and objects - Organization of multiple files: modulemodule and packagepackage
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 22 Python Implementations of Classes and Objects - Exceptions and Syntax Errors
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 23 Python Implementation of Classes and Objects - Throwing Exceptions
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 24 Python Implementations of Classes and Objects - Exception Catching and Handling
The best object-oriented programming tutorials on the web for getting started: 25 Python implementation of classes and objects - Python to determine the type of input data
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 26 Python Implementations of Classes and Objects - Context Managers and with Statements
The best introductory object-oriented programming tutorials on the web: 27 Python implementation of classes and objects - Exception hierarchy and custom exception class implementation in Python
The best object-oriented programming tutorials on the net for getting started: 28 Python implementations of classes and objects - Python programming principles, philosophies and norms in a big summary
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 29 Python Implementations of Classes and Objects - Assertions and Defensive Programming and Use of the help Function
The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 30 Python's Built-In Data Types - the root class of object
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 31 Python's Built-In Data Types - Object Object and Type Type
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 32 Python's Built-in Data Types - Class Class and Instance Instance
The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 33 Python's Built-In Data Types - The Relationship Between the Object Object and the Type Type
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 34 Python's Built-In Data Types - Python's Common Compound Data Types: Tuples and Named Tuples
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 35 Python's Built-In Data Types - Document Strings and the __doc__ Attribute
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 36 Python's Built-In Data Types - Dictionaries
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 37 Python's Common Composite Data Types - Lists and List Derivatives
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 38 Python's Common Composite Data Types - Using Lists to Implement Stacks, Queues, and Double-Ended Queues
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 39 Python Common Composite Data Types - Collections
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 40 Python's Common Compound Data Types - Enumeration and Use of the enum Module
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 41 Python's Common Composite Data Types - Queues (FIFO, LIFO, Priority Queue, Double-Ended Queue, and Ring Queue)
The best introductory object-oriented programming tutorials on the web: 42 Python commonly used composite data types-collections container data type
The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 43 Python's Common Composite Data Types - Extended Built-In Data Types
The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 44 Python Built-In Functions and Magic Methods - Magic Methods for Rewriting Built-In Types
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 45 Python Implementations of Common Data Structures - Chain Tables, Trees, Hash Tables, Graphs, and Heaps
The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 46 Python Function Methods and Interfaces - Functions and Event-Driven Frameworks
The network's most suitable for the introduction of object-oriented programming tutorials: 47 Python function methods and interfaces - callback function Callback
More highlights to watch:
Accelerating Your Python: A Quick Guide to Python Parallel Computing
Understanding CM3 MCU Debugging Principles in One Article
Liver half a month, embedded technology stack summary out of the big
The "Secrets of the Martial Arts" of the Computer Competition
A MicroPython open source project collection: awesome-micropython, including all aspects of Micropython tool library
Avnet ZUBoard 1CG Development Board - A New Choice for Deep Learning
SenseCraft Deploys Models to Grove Vision AI V2 Image Processing Module
Documentation and code acquisition:
The following link can be accessed to download the document:
/leezisheng/Doc
This document mainly introduces how to use Python for object-oriented programming, which requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python object-oriented programming, this document is more detailed and focuses on embedded host computer applications, with common serial port data sending and receiving, data processing, and dynamic graph drawing as application examples for the host computer and the lower computer, and using Sourcetrail code software to visualize and read the code for readers' easy understanding.
The link to get the relevant sample code is below:/leezisheng/Python-OOP-Demo
main body (of a book)
Positional Arguments
Positional parameters are the most common type of parameter, and their values are determined by their position at the time of the function call. The order of the parameters in the function definition determines their correspondence at the time of the function call. Example:
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
greet("Alice", 30)
# exports: Hello Alice, you are 30 years old.
In this example, thename
cap (a poem)age
is the positional parameter.greet
The function is called with the"Alice"
homologousname
,30
homologousage
。
Keyword Arguments
Keyword parameters allow you to explicitly specify the name of the parameter when you call the function. They can be mixed with positional parameters, but keyword parameters must be supplied after positional parameters.
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
greet(name="David", age=28)
# exports: Hello David, you are 28 years old.
In this example, thename
cap (a poem)age
are passed as keyword arguments. This improves code readability and allows you to specify arguments in any order when calling the function.
Default Arguments
Default parameters allow you to provide default values for parameters at function definition time. If these parameters are not passed when the function is called, the default values are used. Example:
def greet(name, age=25):
print(f"Hello {name}, you are {age} years old.")
greet("Bob")
# exports: Hello Bob, you are 25 years old.
greet("Charlie", 40)
# exports: Hello Charlie, you are 40 years old.
In this example, theage
parameter has a default value25
. If you call thegreet
function without providing theage
value, it will use this default value.
Variable parameters
In Python, in addition to the traditional positional, keyword, and default passes, there are also variable parameter passes and variable keyword passes, which are explained next. In the following example, we define a function that prints two strings passed into the function:
def print_str(first, second):
print(first, second)
print_str("hello", "world")
If you pass one argument to the call, print_str("hello"), then an exception is bound to be thrown. If we're not sure in advance about the number of strings we need to pass, this can be accomplished using Python variable parameters. A variable parameter is a parameter that can take any number of arguments. In Python, variable parameters can be defined by prefixing the parameter name with an asterisk * so that the function can accept any number of positional arguments.
Variable arguments are commonly accepted in function definitions by *args, where * is specified and args can be replaced by other names, but it is generally customary to use args. Variable arguments, when passed into a function, are encapsulated into a tuple for use.So we can manipulate the parameters by manipulating the tuple inside the function. The sample code is as follows:
def print_str(first_str, *args):
print(type(args))
print(args)
print(first_str)
for n in args:
print(n)
print_str("hello", "world","i","am","coming")
The following is the result of the run:
If you have defined a list or tuple outside the function and you want to call a variable parameter, you can also use *.+ variable name(this usage is somewhat similar to pointers in C), in this case, when the function is called, an unwrapping function is performed on the incoming tuple parameters, and each element is automatically used as a positional parameter of the function. The example is as follows:
def print_str(first_str, *args):
print(type(args))
print(args)
print(first_str)
for n in args:
print(n)
l=["world","i","am","coming"]
print_str("hello",*l)
The results of the run are as follows:
Variable parameters allow you to pass 0 or any number of parameters that are automatically assembled into a tuple when the function is called, while variable keyword parameters allow you to pass 0 or any number of parameters with names that are automatically assembled into a dict inside the function.
Variable keyword arguments are identified using kwargs, which are used to extend the functionality of a function. For example, if we want to implement user registration with mandatory and non-mandatory entries, these non-mandatory entries can be accepted with variable keyword arguments.
The sample code is as follows:
def register(name, email, **kwargs):
print("name: {}, age: {}, others: {}".format(name, email, kwargs))
for i in kwargs:
print(str(i)+":"+kwargs[i])
register("demon", "1@", addr="shanghai",gender="male", birthday="2022-03-31")
The results of the run are as follows:
For keyword arguments, the caller of a function can pass in any unrestricted number of keyword arguments. Exactly which ones are passed in needs to be checked inside the function with kwargs. If you want to restrict the names of keyword arguments, you can use named keyword arguments, e.g., only accept addr and gender as keyword arguments. A function defined this way would look like this:
def register(name, email,*,addr,gender):
print("name: {}, age: {}, addr: {}, gender: {}".format(name, email, addr,gender))
register("demon", "1@", addr="shanghai",gender="male")
Unlike keyword parameters **kw, named keyword parameters require a special separator, and the parameters that follow are considered named keyword parameters. Named keyword parameters must be passed a parameter name, unlike positional parameters.When defining a function, the parameter order must be: mandatory (positional) parameters -> default parameters -> variable positional parameters -> variable keyword parameters.