1. Function Methods
A function is an organized, reusable snippet of code used to implement a single or related piece of functionality. Functions improve an application’s modularity and the reuse rate of code. Python provides many built-in functions, such as print() and str(). At the same time, Python also allows users to create functions, and these are called custom functions.
Python uses the def keyword to define a function, writing in order the function name, the parentheses, the parameters inside the parentheses, and a colon:, then writing the function body in an indented block; the function’s return value is returned with the return statement. A method defined inside a Python class is called a class function method. Let’s look at an example:
| |
类 A 的实例方法
静态方法
类方法
类方法
A function with a function name, as above, is called a named function. Python also supports anonymous functions, for example, lambda x: x * x. Whether it is a named function or an anonymous function, it is a collection of statements and expressions. Python functions, like strings and numbers, are objects that have some attributes of their own, which can be inspected with built-in functions.
| |
2. Function Parameters
2.1 fun(a,b,c)
Assign the arguments directly to the parameters, matching by position — that is, strictly requiring the number and positions of the arguments to equal those of the parameters. This is fairly ordinary, and most languages commonly use it.
2.2 fun(a=1, b=2, c=3)
Match arguments to parameters in key-value pair form. This style lets you ignore the positional relationship of the parameters and assign directly by keyword. Another advantage of this passing style is that when calling the function you do not require the number of arguments to be equal, i.e. you can call the function as fun(3,4); the key here is that the leading 3,4 override the original values of the two parameters a and b, while c stays unchanged and keeps its original default value 3. This mode is more flexible than the one above: not only can you shuffle the parameter positions with fun(c=5, a=2, b=7), but when no corresponding parameter is passed it falls back to the default value defined when the function was declared.
2.3 fun(*args)
This passing style allows any number of arguments; these several arguments are all put into a tuple and assigned to the parameter args. To use these parameters in the function afterwards, you just operate directly on the args tuple. The advantage is that there is no limit on the number of arguments, but because it is a tuple it is itself still ordered, so a certain constraint remains, and there is also some inconvenience when operating on the arguments.
2.4 fun(**kargs)
The most flexible of all: it passes arguments to the function in the form of a key-value dictionary, combining the positional flexibility of the second style with the unlimited count of the third.
2.5 Mixed Arguments and Examples
fun(arg1, arg2, *args , **kargs), but when mixing the four styles you must obey:
- *args must come after arg=value
- **kargs must come after *args
- The assignment process is: in order, assign the passed arguments to the corresponding parameters; arguments of the form args = value are assigned to parameters; the surplus arguments are packed into a tuple and passed to *args; and the surplus key=value arguments are packed into a dicrionary and passed to **kargs.
| |
| |
1
2
()
{}
| |
1
2
(3,)
{}
| |
1
2
(3,)
{'a': 4}
3. Function Methods
A Python class provides three kinds of methods: instance methods (instancemethod), static methods (staticmethod), and class methods (classmethod). Of course, in Python you can also declare abstract methods with @abc.abstractmethod from the abc module.
3.1 Instance Methods
When defined, the first parameter is always self. It can only be called as instance_name.method_name, and it can access class attributes, instance attributes, class methods, instance methods, and static methods.
Instance methods are tightly related to instances; they are mainly used to change and get the state of an instance, and to operate on the instance.
Use cases: methods related to an instance object, such as initialization, modification, getting instance object attributes, and so on.
| |
3.2 Static Methods
It has no required parameters and is decorated with the staticmethod decorator. It can be called as instance_name.method_name, and also as class_name.method_name. It cannot access instance attributes or instance methods.
A static method does not access the class or the object itself; it is merely a function.
Use cases: methods that belong to the class but do not use any information about the class, such as data validation, algorithms, and so on.
| |
3.3 Class Methods
When defined, the first parameter is always cls, and it is decorated with the classmethod decorator. It can be called as instance_name.method_name, and also as class_name.method_name. It cannot access instance attributes or instance methods, and it supports creating objects from initial data in a specific format.
A class method is not bound to an instance object, but to the class.
Use cases: factory methods used to create class instances can use class methods
| |
