Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: handling return codes from CTYPES

Thread Tools

Re: handling return codes from CTYPES

Mike C. Fletcher
Posts: n/a
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

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
handling return codes from CTYPES Steve Simmons Python 2 01-22-2013 01:10 PM
Re: handling return codes from CTYPES MRAB Python 0 01-21-2013 08:21 PM
Re: handling return codes from CTYPES Steve Simmons Python 0 01-21-2013 04:52 PM
Re: handling return codes from CTYPES Mike C. Fletcher Python 0 01-21-2013 03:14 PM
Virtual Key Codes, Scan Codes and ASCII Codes in C C Programming 2 08-20-2005 11:04 AM