<div dir="ltr">It is possible that this is <a href="https://pypi.python.org/pypi/recordclass">recordclass</a> <wink><br>A short <a href="http://nbviewer.ipython.org/urls/bitbucket.org/intellimath/recordclass/raw/default/examples/what_is_recordclass.ipynb">example</a> to illustrate that fact.<br><br>суббота, 28 марта 2015 г., 16:37:40 UTC+3 пользователь Steven D'Aprano написал:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Fri, Mar 27, 2015 at 04:13:46PM -0700, Andrew Barnert wrote:
<br>> On Mar 27, 2015, at 06:22, Joao S. O. Bueno <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="AbAkl0pBGzMJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">jsb...@python.org.br</a>> wrote:
<br>[...]
<br>> > The Python equivalent of a C Struct.
<br>>
<br>> But a C struct is not a subtype of, or substitutable for, a C array.
<br>> It's not indexable. And the same is true with the equivalents in other
<br>> languages. In fact, the dichotomy between struct--heterogeneous
<br>> collection of fixed named fields--and array--homogeneous collection of
<br>> indexed fields--goes back way before C. So, if you want the equivalent
<br>> of a C struct, there's no reason to make it an iterable in Python.
<br>
<br>Joao said "The Python equivalent of a C struct", not "a C struct".
<br>Python is not C, and Python data types are not limited to what C does.
<br>Python strings aren't limited to C null-delimited strings, and Python
<br>ints aren't limited to what C ints can do.
<br>
<br>I think the original thread was quite explicit about what is wanted:
<br>something like a mutable equivalent to namedtuple. Namedtuples are used
<br>in Python where C would use a struct, or Pascal a record, except that
<br>namedtuples (being tuples) are immutable. I think it's quite reasonable
<br>to want a mutable version.
<br>
<br>Effectively, namedtuple is just a convenience function for wrapping up a
<br>bunch of nice-to-have but not essential functionality around an
<br>immutable struct. Python got by with unnamed tuples for over a decade,
<br>so it's not like we *have* to have namedtuples. But having got them,
<br>would we go back to using regular tuples as a struct? Hell no. Having
<br>named fields is so much better.
<br>
<br>
<br>
<br>> And a class already is the Python of a C struct, it's just that it can
<br>> do _more_ than a C struct.
<br>
<br>This is why it is unfair to insist that a Python equivalent of a C
<br>struct be limited to what C structs do.
<br>
<br>
<br>> > Just that.
<br>> > An easy to create class, with named fields,
<br>>
<br>> Which is easy to do: just create a class, and create its fields in the
<br>> __init__ method (or, in some cases, it's acceptable to use class
<br>> attributes as "default values" for instance attributes).
<br>
<br>If this is so easy, why we have namedtuple *and* SimpleNamespace
<br>in the standard library. Are they both mistakes?
<br>
<br>SimpleNamespace is especially interesting. The docs say:
<br>
<br>"However, for a structured record type use namedtuple() instead."
<br>
<br><a href="https://docs.python.org/3/library/types.html#types.SimpleNamespace" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Ftypes.html%23types.SimpleNamespace\46sa\75D\46sntz\0751\46usg\75AFQjCNG6lIrEiwpXeEOfBpYpff2dyf3hCg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Ftypes.html%23types.SimpleNamespace\46sa\75D\46sntz\0751\46usg\75AFQjCNG6lIrEiwpXeEOfBpYpff2dyf3hCg';return true;">https://docs.python.org/3/<wbr>library/types.html#types.<wbr>SimpleNamespace</a>
<br>
<br>
<br>which is great if you want an *immutable* structured record type, but
<br>not if you want a mutable one.
<br>
<br>Which brings us back to where this thread started: a request for a
<br>mutable version of namedtuple. That's trickier than namedtuple, because
<br>we don't have a mutable version of a tuple to inherit from. Lists won't
<br>do the job, because they have a whole lot of functionality that are
<br>inappropriate, e.g. sort, reverse, pop methods.
<br>
<br>That makes it harder to create a mutable structured record type, not
<br>simpler.
<br>
<br>Think about the functional requirements:
<br>
<br>- it should be semantically a struct, not a list or array;
<br>
<br>- with a fixed set of named fields;
<br>
<br>- fields should be ordered: a record with fields foo and bar is not the
<br>same as a record with fields bar and foo;
<br>
<br>- accessing fields by index would be a Nice To Have, but not essential;
<br>
<br>- but iteration is essential, for sequence unpacking;
<br>
<br>- values in the fields must be mutable;
<br>
<br>- it should support equality, but not hashing (since it is mutable);
<br>
<br>- it must have a nice repr and/or str;
<br>
<br>- being mutable, it may directly or indirectly contain a reference to
<br>itself (e.g. x.field = x) so it needs to deal with that correctly;
<br>
<br>- support for pickle;
<br>
<br>- like namedtuple, it may benefit from a handful of methods such as
<br>'_asdict', '_fields', '_make', '_replace' or similar.
<br>
<br>
<br>Does this sound easy to write? Well, sure, in the big picture, it's
<br>hardly a 100,000 line application. But it's not a trivial class.
<br>
<br>
<br>
<br>--
<br>Steve
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="AbAkl0pBGzMJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\46sa\75D\46sntz\0751\46usg\75AFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a>
<br></blockquote></div>