- Hands-On Penetration Testing with Python
- Furqan Khan
- 1198字
- 2021-07-02 14:13:55
Functions and methods in Python
Functions and methods are used to design or make a logical unit of code that can be reused throughout the course of your script or other scripts. Functions actually form the basis of code reuse and bring modularity to the code structure. They keep the code clean and easier to modify.
The following code represents the basic syntax of defining methods in Python:
def print_message(message):
print(message)
statement 2
statement
Python methods do not have a return type in their definition as you might have seen in C, C++, or Java, such as void, in, float, and so on. A Python method may or may not return a value, but we do not explicitly need to specify that. Methods are very powerful and flexible in Python.
Let's explore the various ways of invoking methods using our method_basics.py script:

Let's now break this down into smaller pieces and try to understand what has happened:
- print_msg1(): This is a basic method that just prints a string on the console. It is defined at line 2 and invoked at line 19.
- print_msg2(): This is a method that takes an argument in the variable message and then prints that variable value on the screen. Remember that Python variables do not require a type to be specified, so we can pass any data to the message variable. This is a Pythonic example of a method that takes a single argument. Remember that the type of the argument is a Python object and it can take any value passed to it. The output can be seen in the following screenshot:

- print_msg3(): This is a Python method that takes two arguments. It is similar to the print_msg2() method that we saw previously. The difference is that it may sometimes return a value. It is also invoked differently. Note that in line 22, we invoke this method by passing the second parameter as True. This means it has a return value of True, but we do not invoke it with True as a second parameter in line 26, so it therefore returns nothing. For this reason, we get None printed on the screen. In other programming languages, such as C, C++, or Java, the order of parameters while invoking the method is very important. This is because the sequence with which we passed the argument should be the same sequence that is passed to the method. In Python, however, we can invoke the methods and pass the named parameters during invocation. This means that the order or sequence doesn't matter, as long as the name matches the name of the method parameter. This is depicted in line 29, where we are passing a message as a second parameter, even though it is the first parameter in the method definition. This works perfectly, as shown in the output.
- print_msg4(): This is where we get familiar with Python default parameters and how they can be used with methods. A default parameter is a variable that is assigned a default value while a method is declared. If the caller passes on a value for this parameter or variable, then the default value is overwritten by the value passed by the caller. If no value is passed for the default parameter during invocation, then the variable persists the default value to which it was initialized. The print_msg4() method has got one mandatory argument, m, and two optional arguments, op1 and op2.
- print_msg4('Test Mandatory'): This is invoked at line 31. This indicates that the Test mandatory string should be passed for the mandatory parameter and the other two op1 and op2 variables will be initialized to the default values, as seen in the output.
- print_msg4(1,2): This is invoked at line 32. This indicates that an integer with value=1 should be passed for the mandatory parameter and another integer with value=2 should be passed for op1. The default value will therefore be overwritten for op1. op2 will retain the default value, as no value is passed.
- print_msg4(2,3,2): This is invoked at line 33. This indicates that an integer with value=2 should be passed for the mandatory parameter and another integer with value=3 should be passed for op1 so the default values for op1 and op2 are overwritten.
- print_msg4(1,op2='Test'): This is invoked at line 34. The mandatory parameter receives an integer with value=1. For the second parameter, we are specifying a named parameter during invocation, so the sequence does not matter for Test, which will get copied to op2 of the caller.
- print_msg4(1,op2=33,op1=44): This is invoked at line 35. The mandatory parameter receives value=1. For the second parameter, we specify a named parameter, op2, and for the third parameter, we pass op1. Again, we can see in the output that the sequence does not matter.
- print_msg5(): Usually, in other programming languages, a function or method can always return one value. If it needs to return multiple values, it must put the values in an array or another structure and then return them. Python handles this situation abstractly for us. If you read the code, you might think that the method is returning multiple values, whereas in reality it's returning a tuple with each value multiplied by two. This can be validated from the output.
Let's now explore some further methods and ways to pass arguments, using the following example, methods_adv.py. The following code snippet represents variable-argument type methods in Python. As can be verified from the output, method_1 takes a normal sequence of any size as an input, meaning we can pass any number of arguments to the method. When the method is declared with a parameter preceded by the * sign, all the passed arguments are translated into a sequence and a tuple object is placed inside args. On the other hand, when * is used with a parameter while invoking the method, the parameter type from the sequence is changed and internally each element if sequence is passed as a single parameter to the caller, as seen in method_1_rev.
Furthermore, when if is used with a parameter in the method declaration, it internally transforms all the maned parameters into a Python dictionary, with the key as the name and the value being the same as the value after the = operator. This can be seen in method_2. Finally, when ** is used with the caller parameter, that parameter is internally transformed from a Python dictionary to named parameters. This can be validated with method_2_rev:

- PHP基礎案例教程
- JavaScript從入門到精通(第3版)
- 零基礎學Java程序設計
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(高級進階篇)
- OpenCV with Python By Example
- Java并發編程:核心方法與框架
- Web Developer's Reference Guide
- Getting Started with Python
- Natural Language Processing with Python Quick Start Guide
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Swift High Performance
- Java程序設計教程
- Joomla!Search Engine Optimization
- Microsoft Dynamics GP 2013 Cookbook
- 啊哈C語言?。哼壿嫷奶魬穑ㄐ抻啺妫?/a>