0

I'm programming in python for a while. Most often I need to define structures like in C such as

struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

how can I do it in python.

  • Python strings are not character arrays, they can't be a fixed length, Python variables can't have types declared. `Books = {}`, `class Books(object): pass`, `class Books(object): __slots__='title','author','subject','book_id'`, and [ctypes.structure](http://stackoverflow.com/q/4351721/478656) are all potentially valid translations, depending what, specifically, you want to achieve. – TessellatingHeckler Jun 21 '16 at 18:36
  • From your example it really looks like you don't need a struct, but a [class](https://docs.python.org/3/tutorial/classes.html). – marcelm Jun 21 '16 at 19:01
  • Possible duplicate of [C-like structures in Python](http://stackoverflow.com/questions/35988/c-like-structures-in-python) – Padraic Cunningham Jun 21 '16 at 19:16
  • This topic is already mention on stackoverflow at [link](http://stackoverflow.com/questions/35988/c-like-structures-in-python) – D. Kamer Jun 21 '16 at 20:30

0 Answers0