Emulating C structs in PythonOne of the few data types from C that I miss when writing Python code
is If you want class MyStruct: pass ms = MyStruct() ms.foo = 10 ms.bar = "abc" Some people will say that this is an abuse of objects, since they don't have any code, just data. I say to heck with such people; sometimes all I want is data. (Avoid the temptation to just use ' Initialization this way is tedious, though. We can do it easier and more compactly by using keyword arguments when we create the object, with a little help from the class. Like so:
(And look, now our objects have some code.) It's possible to write the (I am absolutely sure people have been using this idiom for years before I got here.) Sidebar: dealing with packed binary dataIf you need to deal with packed binary data in Python, you want the struct module. This is a much better tool than C has, because (I sometimes find it ironic that supposedly 'high level' languages like Python and Perl have better tools to deal with binary structures than 'low level' C.) (2 comments.)
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |