Python Forum
Passing a local variable outside of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing a local variable outside of a function
#1
I have a sort of specific question today. I want to know how, in the specific case of my code, how I can pass a function's local variable outside the function?
Code:
def folder_exists(folder):
    enum_func = ctypes.WINFUNCTYPE(wintypes.BOOL,
                                   wintypes.HWND,
                                   wintypes.LPARAM)

    def query(hwnd, lParam):
        found = 0
        length = ctypes.WinDLL('User32').GetWindowTextLengthW(hwnd) + 1
        buf = ctypes.create_unicode_buffer(length)
        ctypes.WinDLL('User32').GetWindowTextW(hwnd, buf, length)
        if buf.value == folder:
            found += 1
        return True

    worker = enum_func(query)
    ctypes.WinDLL('User32').EnumWindows(worker, None)
    print(found)

folder_exists('Downloads')
The query function needs to return true in order for the C function call "EnumWindows" to work. Basically what I'm trying to do here is 'do something' (don't worry about what) once a certain folder title is found. So how can I or what can I pass as some sort of indicator that the correct folder was found from the query function? Is there some way I can pass the found local variable outside the function? I needs to be whatever it's set to when the function is called.
Reply
#2
What about return [found]? Since it's a non-empty list, it will evaluate as True, which should satisfy EnumWindows. However, it also contains the value you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The problem with that is the function is sort of a special case that really should only be called with the call to "EnumWindows". It won't give an accurate result otherwise and found won't be accurate.
Reply
#4
I don't understand the WinDLL part but you could try a callable object
def folder_exists(folder):
    enum_func = ctypes.WINFUNCTYPE(wintypes.BOOL,
                                   wintypes.HWND,
                                   wintypes.LPARAM)
    class Query:
        def __call__(self, hwnd, lParam):
            self.found = 0
            length = ctypes.WinDLL('User32').GetWindowTextLengthW(hwnd) + 1
            buf = ctypes.create_unicode_buffer(length)
            ctypes.WinDLL('User32').GetWindowTextW(hwnd, buf, length)
            if buf.value == folder:
                self.found += 1
            return True
    query = Query()
    worker = enum_func(query)
    ctypes.WinDLL('User32').EnumWindows(worker, None)
    print(query.found)
 
folder_exists('Downloads')
Reply
#5
Thanks, but didn't work. found printed as 0 where it should have been 1. To clarify, it's the call enum_func that needs a boolean value to be returned from the query function, not "EnumWindows". "EnumWindows" needs the function prototype "WINFUNCTYPE".
Reply
#6
malonn Wrote:found printed as 0 where it should have been 1.
How do you know it should have been 1? Did the code execute the self.found += 1 ? Try to raise a RuntimeError at this point to see what it does.
Reply
#7
It should be 1 because the code checks to see if a window with the title "Downloads" is open, which it is. And yes, self.found += 1 was executed.

I'm a noob, how would I raise a runtimeerror?

A Global will work, but from what I read, globals are bad form.
Reply
#8
raise RuntimeError('useful message here')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
I figured it out--thanks, ichabod & Gribouillis. I ended up using nested functions and nonlocal. Here's the code for any interested:

def get_folder(name_in):
    folder = ''
    enum_func = ctypes.WINFUNCTYPE(wintypes.BOOL,
                                   wintypes.HWND,
                                   wintypes.LPARAM)

    def names_callback(hwnd, lParam):
        nonlocal folder
        length = ctypes.WinDLL('User32').GetWindowTextLengthW(hwnd) + 1
        buf = ctypes.create_unicode_buffer(length)
        ctypes.WinDLL('User32').GetWindowTextW(hwnd, buf, length)
        if buf.value == name_in:
            folder = name_in
        return 1

    worker = enum_func(names_callback)
    ctypes.WinDLL('User32').EnumWindows(worker, None)
    return folder
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  It's saying my global variable is a local variable Radical 5 1,155 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Printing the variable from defined function jws 7 1,283 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 936 Aug-07-2023, 05:58 PM
Last Post: Karp
  passing dictionary to the function mark588 2 972 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,039 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,910 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Cant transfer a variable onto another function KEIKAS 5 1,880 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  passing php variable to python file jerald 1 2,679 Jul-07-2021, 11:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020