Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: handling return codes from CTYPES (

Mike C. Fletcher 01-21-2013 08:31 PM

Re: handling return codes from CTYPES
On 13-01-21 11:52 AM, Steve Simmons wrote:
> Mike,
> Thanks for your response - I was puzzled by one part of it though...
> On 21/01/2013 15:14, Mike C. Fletcher wrote:
>> That's because you've just discarded the object you created

> I (mis?)understood from the ctypes documentation that '>>> initResult
> = c_short(0)' would result in the creation of a ctypes 'short' called
> initResult and that this object would be mutable. On that basis, I
> expected the following statement '>>> initResult = initScanLib( ... )'
> would assign the result of the call to initResult.
> So, I can understand that I am not using the correct approach but I
> don't understand how I discarded the object I created. Can you
> clarify please?

Sure, the problem isn't here a ctypes issue, but a Python one. When you
do the following:

>>> initResult = c_short(0)

you have bound the name "initResult" (a string key in a "namespace"
dictionary) to a ctypes c_short object. The name "initResult" is in no
way special, nor is there any "type" associated with that variable
name. The fact that it has been assigned to a c_short *at this moment*
does not affect any *future* value assigned to that name.

>>> initResult = initScanLib( ... )

Here you have assigned whatever object is the result of initScanLib( ...
) to the name "initResult". The old object pointed to by "initResult"
is no longer referenced by that name, so the Python intepreter is free
to discard that old object immediately. Thus you have "discarded" the
object you created by reassigning the single reference to it to another
object, which leaves it free to be garbage collected (depending on
Python implementation that might be instantly or eventually).

Python does not have typed *variables*, every variable is a pointer to
an object (PyObject *) under the covers. There is nothing in Python like:

int i;
short j;

nothing which makes a variable type-enforcing or constrained. At least,
nothing in core Python. In theory you can create a namespace which
*does* allow such things, but that's getting pretty involved.

Hope that helps,

Mike C. Fletcher
Designer, VR Plumber, Coder

All times are GMT. The time now is 01:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.