title: How to store a pointer in a custom embedded Python object
body: <p>I have created a custom Python type in C as per the tutorial <a href="https://docs.python.org/2.7/extending/newtypes.html#the-basics" rel="nofollow noreferrer">https://docs.python.org/2.7/extending/newtypes.html#the-basics</a>. In my C I receive a pointer to a struct, I want to be able to get and set the values in the struct from Python without taking a copy of it. I.e.</p>
  
  <pre><code>a = myObject.x() # gets the x value in the struct.
  </code></pre>
  
  <p>or</p>
  
  <pre><code>myObject.x(255) # sets the x value in the struct.
  </code></pre>
  
  <p>However I cannot see how to store the pointer in the python object.</p>
  
  <p>My current object definition is currently just the basic object implementation from the python website.</p>
  
  <pre><code>typedef struct {
      PyObject_HEAD
      myStruct *s;
  } KeyObject;
  
  static PyTypeObject KeyType = {
      PyVarObject_HEAD_INIT(NULL, 0)
      "ckb.Key",             /* tp_name */
      sizeof(KeyObject), /* tp_basicsize */
      0,                         /* tp_itemsize */
      0,                         /* tp_dealloc */
      0,                         /* tp_print */
      0,                         /* tp_getattr */
      0,                         /* tp_setattr */
      0,                         /* tp_compare */
      0,                         /* tp_repr */
      0,                         /* tp_as_number */
      0,                         /* tp_as_sequence */
      0,                         /* tp_as_mapping */
      0,                         /* tp_hash */
      0,                         /* tp_call */
      0,                         /* tp_str */
      0,                         /* tp_getattro */
      0,                         /* tp_setattro */
      0,                         /* tp_as_buffer */
      Py_TPFLAGS_DEFAULT,        /* tp_flags */
      "Key objects",           /* tp_doc */
  };
  
  static PyMethodDef key_methods[] = {
      {NULL}  /* Sentinel */
  };
  </code></pre>