7 ms·
It would be nice if Python had a fixed length but mutable sequence type. MutableTuple anyone?
by nerdponx 8y ago
It would be nice if Python had a fixed length but mutable sequence type. MutableTuple anyone?
- cdancette 8y agoWhy do you have the fixed length requirement? Regular lists seem to work perfectly for this
- dkersten 8y agoPresumably so you don’t have to pay for the memory or performance overhead of supporting resizing? Possibly also to more closely match the semantics of the code with what you want to do, to make it easier to catch errors. I find Pythons core data structures really convenient and easy to use, but sometimes the C++ programmer in me wishes that I had more options available to me (eg, Python has lists and tuples, C++ has double ended linked lists, arrays, tuples, vectors, double ended vectors in the standard library), the idea being that I can choose something that is a very close match to usage patterns. Having said that, I’ve never actually found it to be a limitation in real Python code and when memory or performance matters enough that these differences start to show, Python is usually no longer the best tool for the job anyway, so it doesn’t really need these things. Still, sometimes I feel like I want them anyway, probably just because I’ve done enough C++ to be used to having them :)
- heavenlyblue 8y agodouble ended linked lists - deque arrays - array, numpy arrays vectors - numpy again
- dkersten 8y agoSure, with third party libraries (that are mostly written in C), you can get whatever you want. I’m just talking part of the language. In C++, the range of data structures are huge if I use third party libraries. For example, EASTL expands the STL containers with intrusive variants and more. Of course, an argument could be made that looking just at the language (without third party libraries) is unrealistic and useless, and its true. You could also argue that Python libraries are easier to install and use than C++ libraries (which is also true), but when you look at the languages in isolation, Python only has a few basic structures and C++ has multiple ones. Again, I acknowledge that this is an academic discussion since in real life, I’ve never actually seen it matter and, when it does, libraries like numpy that delegate the work to C exist and are easy to use.
- heavenlyblue 8y agoI don't know: if you really need to do some heavyweight maths in C++, you're going to go the CUDA/heavyweight intrinsic way anyway. I don't yet see how STL containers (excuse me for the pun) contain the performance problem at it's limit - but otherwise why would you use C++? Also, having these specific structures as part of the standard library would make you think that what you're doing is acceptable: thinking about the overhead of a doubly-linked-list when your variable assignment operation requires an access to a Namespace object based on a variation of a HashMap. Seriously: whenever you need a fast solution, Python doesn't stop you from building something in C++. But trying to make Python into C++ is either an incredibly long shot or just having no idea what Python is actually useful for.
- collyw 8y agoWhat scenarios would it be useful for?
- coldtea 8y agoWherever you want to store precisely N attributes, but also want to be able to mutate them. E.g. points on a plane (x,y) and an algorithm that sums them to find some center etc.
- js2 8y agoUse a class with __slots__ for that.
- deleted 8y ago[deleted]
- orf 8y agoThis is the correct answer for this if you're using Python and have lots of small objects with fixed attributes. In [1]: class A: ...: __slots__ = ('a', 'b') ...: def __init__(self): ...: self.a = 1 ...: self.b = 2 ...: In [3]: get_size(A()) Out[3]: 112 In [4]: get_size((1,1)) Out[4]: 120 Smaller than a tuple. Not to say that 112 bytes is great, but __slots__ can really help with memory usage. Without: In [23]: get_size(A()) Out[23]: 324 A named tuple, while not mutable, is 92 bytes with those two attributes.
- joshuamorton 8y agoMany python-ists have strong opinions on this, and they're fun to type, so I'll repeat mine. A type-homogeneous, variable length sequence (List[int]) should be a list/generator/etc. or in some cases a tuple (for example if it was declared explicitly). Other great places for a tuple: the default argument for something that takes in a Sequence, since defaulting to an empty list can be dangerous. However, for immutable, fixed-length, type-heterogenous sequences (which is where tuples are often used, implicitly as return types etc), a namedtuple (python's lightweight record type) is almost always superior (the only question is "is it worth it to define this"), and for mutable, fixed-length, type-heterogenous sequences (ie. points you can modify), dataclasses/attrs (python3.7 standard library dataclasses, attrs in python2.7+) are almost certainly preferable. Basically, when you have a known number of attributes, you can give them names better than `[0]` and `[1]`.
- omaranto 8y agoThe length of a list is fixed if you don't use any operation that changes the length of the list. :)
- dagw 8y agoNamedTuple has a _replace method (that you probably shouldn't be using) that you could use to build something like that if you really need it.
- js2 8y agoThe _replace method returns a new tuple. The existing tuple is not changed. There's no reason not to use that method. It's part of namedtuple's formal API. The reason for the underscore is so that the method name won't collide with a field. (All of namedtuple's methods use a leading underscore.)
- jastr 8y agomy_fixed_array = [None] * 10
- srean 8y agoIts not in the language, but a numpy array will do the job.
- JoBrad 8y agoA deque can have a fixed length, and its elements are mutable. https://docs.python.org/3.6/library/collections.html#collections.deque https://docs.python.org/3.6/library/collections.html#collect...