python what does return do
Tell me, what exactly does return return and how to access it?
import ctypes
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(buff.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
def main():
for t in titles:
if t != '':
print(t)
return titles
if __name__ == '__main__':
main()
I understand that prints are running != empty windows. But, if I remove return in def main (): the result does not change.
2 answers
return
returns the value from the function.
For example, in your version in the function
def main():
for t in titles:
if t != '':
print(t)
return titles`
There is an instruction to output the data print(t)
if they are not equal. "
In this case, you already have data output when executing the function itself. And accordingly, return
has no meaning in the function.
If you want to collect data in a function and then output it, you can use return
.
Suppose you want to not output the values immediately, and collect them separately.
titles = [1, "", 4, 5]
titles1 = []
def main():
for t in titles:
if t != '':
titles1.append(t) ##Если значение не пустое, добавляем в новый массив, вместо вывода на экран
return titles1
print(main()) ## При вызове функции вернётся значение указанное в return функции
Here you come to the store, put money on the counter (function arguments), the cashier (function) took them, did something and put (return) the product on the counter. You can take it or not take it. If you do not take the goods, then of course there is no difference whether the cashier put it or not.