Catalog
For - Else
else clause
Use C Expand
CTypes
Loops are an essential element of any language . similarly ,for The cycle is Python An important part of . However, there are some things that beginners don't know . We will discuss one by one .
Let's start with what we already know . We know it can be used like this for loop :
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
print(fruit.capitalize())
Output: Apple
Banana
Mango
This is a for Loops are very basic structures . Now let's move on ,Python Of for Some little-known features of the cycle .
for There's another cycle else clause , Most of us are not familiar with . This else Clauses are executed at the normal end of the loop . It means , The loop did not encounter any break. Once you know when and where to use it , It will really be very useful . I hate to see it myself .
A common construct is to run a loop , And find an element . If this element is found , We use break To break the loop . There are two scenarios that stop the loop .
Now we may want to know which one of them , That's what makes the loop complete . One way is to set ⼀ A sign , Then mark the end of the loop . The other is to use else clause .
This is it. for/else The basic structure of the cycle :
for item in container:
if search_something(item):
# Found it!
process(item)
break
else:
# Didn't find anything..
not_found_in_container()
Consider this simple case , I took it from the official documents :
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n/x)
break
It will find out 2 To 10 The factor between numbers . Now it's the fun part . We can add an additional else Sentence block , To grasp prime numbers , And tell us :
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print( n, 'equals', x, '*', n/x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
CPython It also implements an interesting feature for developers , Use Python You can easily call C Code
There are three ways for developers to create their own Python Code to call C Functions written ctypes,SWIG,Python/C API. Each approach also has its own advantages and disadvantages .
First , We need to be clear about why we're here Python Call in C?
Common reasons are as follows :
Python Medium ctypes Modules may be Python call C The simplest of the methods .ctypes The module provides and C Language compatible data types and functions to load dll file , Therefore, there is no need to make any changes to the source file when calling . That's how the simplicity of this method is established .
Examples are as follows
To achieve the sum of two numbers C Code , Save as add.c
//sample C file to add 2 numbers - int and floats
#include <stdio.h>
int add_int(int, int);
float add_float(float, float);
int add_int(int num1, int num2){
return num1 + num2;
}
float add_float(float num1, float num2){
return num1 + num2;
}
The following will C The file is compiled as .so file (windows for DLL). The following operation will generate adder.so file
#For Linux
$ gcc -shared -Wl,-soname,adder -o adder.so -fPIC add.c
#For Mac
$ gcc -shared -Wl,-install_name,adder.so -o adder.so -fPIC add.c
Now in your Python Code to call it
from ctypes import *
#load the shared object file
adder = CDLL('./adder.so')
#Find sum of integers
res_int = adder.add_int(4,5)
print "Sum of 4 and 5 = " + str(res_int)
#Find sum of floats
a = c_float(5.5)
b = c_float(4.1)
add_float = adder.add_float
add_float.restype = c_float
print "Sum of 5.5 and 4.1 = ", str(add_float(a, b))
Output is as follows :
Sum of 4 and 5 = 9
Sum of 5.5 and 4.1 = 9.60000038147
In this case ,C The document is self explanatory , It contains two functions , It realizes the sum of shaping and floating-point respectively .
stay Python In file , Start by importing ctypes modular , And then use CDLL Function to load the library file we created . So we can pass the variable adder To use C Functions in the class library . When adder.add_int() When called , Internal will launch a pair of C function add_int Call to .ctypes The interface allows us to call C Function using native Python The default string and integer types in .
For other types like Boolean and floating point , You have to use the right ctype Type is OK . Azimuthal adder.add_float() When a function passes parameters , We need to first Python The decimal value in is converted to c_float type , Then it can be transmitted to C function . Although this method is simple , Clear , But it's Limited . for example , Not in C Operate on objects in .