The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 49 Python function Methods and Interfaces – The Difference Between function and Methods, and Lambda Anonymous Functions
In Python, functions and methods are both basic units of code used to encapsulate and execute specific tasks. There are some important differences between them, and lambda anonymous functions are a method provided by Python for concisely defining small functions.
This document mainly introduces how to use Python for Object Oriented programming, and 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, taking the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples. Meanwhile, it uses the Sourcetrail code software to visualize the code for reading, so as to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Body Text
Functions and Methods
A function encapsulates a set of independent functionalities that can be invoked directly. It can accept some data (parameters) for processing, then return some data (return values), and it is also possible to have no return value. A function can be defined and used directly within a module. All data passed to a function is passed explicitly.
A method is similar to a function in that it encapsulates independent functionality, but a method can only be invoked via a class or an object and represents a targeted operation. The data self and cls in a method are implicitly passed, referring to the caller of the method; meanwhile, a method can manipulate the internal data of a class. Simply put, a function exists independently in Python and can be used directly, whereas a method must be called by something else to take effect.
Simply put, functions that have no binding relationship with classes and instances all belong to functions; functions that have a binding relationship with classes and instances all belong to methods.
Actually, methods of a class do not necessarily have to be implemented inside the class; they can also be declared first within the class without concrete implementation, and then implemented outside the class while assigning values to attributes (i. e., implementing the specific methods). The sample code is as follows:
# Define the Cat class
class Cat:
def say(self):
print("I' m a cat")
cat1 = Cat()
# Define the lie method
lie = lambda self: print("I' m a dog")
# Add the lie method
Cat.lie = lie
# View the attributes of the Cat class
print(dir(Cat))
cat1.lie()
The following is the running result, from which we can see that the lie method has been successfully added to the Cat class.
lambda function/anonymous function
A lambda function is a small, anonymous, inline function that can take any number of arguments but only has a single expression. Anonymous functions do not require the def keyword to define a complete function. Lambda functions are typically used to write simple, single-line functions, and are usually employed in scenarios where a function needs to be passed as an argument, such as in functions like map (), filter (), and reduce ().
Lambda syntax format:
lambda arguments: expression
Parameters of the lambda expression:
lambda is a keyword in Python, which is used to define a lambda function
arguments is a parameter list, which can contain zero or more parameters, but must be specified before the colon (:)
expression is an expression used to compute and return the result of a function
The following example uses lambda to create an anonymous function, which multiplies the parameters a and b and returns the result:
x = lambda a, b : a * b
print(x(5, 6))
lambda functions can also be used together with built-in functions such as map (), filter () and reduce () to perform operations on collections.
For example, to implement the square operation of a list in conjunction with the map () function, the prototype of the map () function is map (function, iterable, . ..), which returns a list as its result. The function applies the function to each element of the iterable and returns the results in the form of a list.
Alternatively, it can be used in conjunction with the filter function to filter elements in a list. The filter () function can select specific elements from an iterable (such as a dictionary or a list) and generate a new iterator. The filter (function, iterable) function returns an iterable filter object, which can be converted into a list using the list () function; this list contains all the items returned by the filter object. In the following example, we filter out the even-numbered items in the list:
or perform operations with the reduce () function, which accumulates elements in the parameter sequence. reduce (function, iterable[, initializer]) performs the following operations on all data in a data collection (such as a linked list, tuple, etc.): first, use the function (function, which takes two parameters) passed to reduce to operate on the 1st and 2nd elements of the collection, then operate the obtained result with the 3rd data using the function, and finally get a single result.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Use reduce() and a lambda function to compute the product
product = reduce(lambda x, y: x * y, numbers)
print(product)