title: Behavior of ctypes.CDLL attribute access
body: <p>I am accessing a function from a DLL on Windows, although I suspect a similar interface applies on Linux. The way I normally see is to store the function object, then assign its <code>argtypes</code> and/or <code>restype</code>, and use it:</p>
  <pre><code>import ctypes
  func = ctypes.windll.kernel32.FreeLibrary
  func.argtypes = [ctypes.wintypes.HMODULE]
  func.restype = ctypes.wintypes.BOOL
  
  # Call func(...)
  </code></pre>
  <p>Now I can use this function to unload DLLs from other sources.</p>
  <p>But what if I set <code>argtypes</code> and <code>restype</code> directly on the attrubute of <code>kernel32</code>? Something like this:</p>
  <pre><code>dll = ctypes.windll.kernel32
  dll.FreeLibrary.argtypes = [ctypes.wintypes.HMODULE]
  dll.FreeLibrary.restype = ctypes.wintypes.BOOL
  
  # Call dll.FreeLibrary(...)
  </code></pre>
  <p>I am aware that these attributes are implemented in <code>ctypes.CDLL</code> as calls to <code>__getitem__</code> (redirected from <code>__getattr__</code>). However, are the types I set this way in <code>argtypes</code>/<code>restype</code> cached, or is a fresh function reference generated every time, like with method binding?</p>
  <p>I have not found any explicit documentation, but I do not see any tutorials using the latter method. So far, I have only been able to trace through some of the code of ctypes. The following <em>appears</em> to work, but I am not sure if it's guaranteed or a one-time coincidence:</p>
  <pre><code>&gt;&gt;&gt; ctypes.windll.kernel32.argtypes = [ctypes.wintypes.HMODULE]
  &gt;&gt;&gt; ctypes.windll.kernel32.argtypes
  [ctypes.c_void_p]  # Correct alias
  &gt;&gt;&gt; ctypes.windll.kernel32.argtypes = [ctypes.c_int]
  &gt;&gt;&gt; ctypes.windll.kernel32.argtypes
  [ctypes.c_long]    # Correct alias
  </code></pre>
  <p><strong>Note</strong></p>
  <p>I understand that Windows DLL references are cached global references. This is just an example to show what happens to the method objects so readers can use a predefined object without over-complicating the MCVE.</p>