0

In the C language, I can save or load a variable to a binary file using fwrite/fread in the following way:

int main()
{
  FILE *fl = fopen("data.bin", "w+");
  FILE *fl2 = NULL;

  struct foo_bar {
    int x;
    char y;
    char *z;
  };

  struct foo_bar a, b;

  a.x = 5;
  a.y = '5';
  a.z = NULL;

  fwrite(&a, sizeof(struct foo_bar), 1, fl);

  fclose(fl);

  fl2 = fopen("data.bin", "r");
  fread(&b, sizeof(struct foo_bar), 1, fl);

  printf("%d %c %d", b.x, b.y, b.z);

  return 0;
}

The key idea that is important to me here is that I don't have to parse a buffer of characters, and then use them to create some object of type

struct foo_bar 

using a constructor, but rather I can just load it directly from the file.

My Question is: Is there a way to accomplish this functionality in Python?


Why would I want to do this?


I need to pass a list of socket objects from a python script to a sub-procedure, and the way my goal is typically accomplished is by first writing the sockets to a file, and then having the sub-procedure read them in from that file. The idea is that the clients which are connected through these sockets can then begin interacting with the sub-procedure, which takes over as the server.

But the best I have been able to do so far in Python is to save the file descriptors of the sockets to a file, load them from the sub-procedure, and then use them, along with the socket's constructor to make new sockets.

But attributes to the underlying _socket object are inaccessible to me, and hence I am unable to recover connectivity with my clients. I hope this makes sense, I have done my best to phrase this as carefully as I can. But if it's unclear please let me know and I'm happy to clarify anything further.

doggo
  • 53
  • 1
  • 6
  • Does this answer your question? [Saving an Object (Data persistence)](https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence) – Brian61354270 Apr 25 '20 at 02:30
  • 2
    It's very important to understand that saving a "variable" doesn't make sense in Python. Python variables and C variables are very different things. C variables are essentially a named location in memory, in Python, a variable is a *name* with no association to any particular location in memory, and that can be used to refer to any Python object. You want to serialize *an object*, not a variable – juanpa.arrivillaga Apr 25 '20 at 02:33
  • @r.ook - I'm not sure, but at first glance it sounds like exactly what I need. I will take a look. Thanks! – doggo Apr 25 '20 at 02:58
  • @juanpa.arrivillaga - point well taken, thanks for the reminder! – doggo Apr 25 '20 at 03:01
  • @juanpa.arrivillaga - the importance of your comment really sunk in when I was out for a walk just now. As a C programmer who is still fresh to Python, this is still sinking in. Sometimes I get it (legitimately), then often a day later I forget and it dawns on me again. The hard distinction between variables and objects seems to be what makes Python what it is – doggo Apr 25 '20 at 05:27
  • @doggon sure, although, most high-level OOP languages are like this, e.g. Java (for "non-primitive types") Ruby, Javascript etc. Generally, you just don't think in terms of "this address, this pointer to another type of variable etc etc", rather, there are names and objects bound to names in namespaces. – juanpa.arrivillaga Apr 25 '20 at 05:55
  • @doggo here's a really great primer on how to understand python on its own terms: https://nedbatchelder.com/text/names.html it's written by a stack overflow legend, so it sort of distills a lot of confusions people get coming from different sorts of languages when they get to python. Python has an inherent simplicity, but you have to understand it. – juanpa.arrivillaga Apr 25 '20 at 06:01

0 Answers0