most recent 30 from softwareengineering.stackexchange.com2025-05-02T01:10:03Zhttps://softwareengineering.stackexchange.com/feeds/tag/data-structures+python+coding-stylehttps://creativecommons.org/licenses/by-sa/4.0/rdfhttps://softwareengineering.stackexchange.com/q/4158627Coupcouphttps://softwareengineering.stackexchange.com/users/3747842020-09-14T01:00:45Z2020-09-14T04:54:38Z
<p>So all four of these approaches to structure data on their surface work more or less the same to keep data well structured.</p>
<p>Are there any reasons, be they hidden performance issues/enhancements, stylistic preferences, or just plain ol' pythonic/un-pythonic ways of doing stuff to prefer one approach over the others?</p>
<pre><code>from typing import NamedTuple
from collections import namedtuple
from dataclasses import dataclass
# Normal class
class X:
def __init__(self, a, b):
self.a: int = a
self.b: int = b
# Normal namedtuple
Y = namedtuple(
'Y',
'a b'
)
# With Inheretence
class Z(NamedTuple):
a: int
b: int
# Dataclass
@dataclass
class A:
a: int
b: int
</code></pre>
<p>All declared the same way</p>
<pre><code>x = X(1, 2)
y = Y(3, 4)
z = Z(5, 6)
a = A(7, 8)
</code></pre>
<p>Accessed the same way</p>
<pre><code>print(x, x.a, x.b)
print(y, y.a, y.b)
print(z, z.a, z.b)
print(a, a.a, a.b)
</code></pre>
<p>With basically the same results</p>
<pre><code><__main__.X object at 0x7c77f9b32e10> 1 2
Y(a=3, b=4) 3 4
Z(a=5, b=6) 5 6
A(a=7, b=8) 7 8
</code></pre>
https://softwareengineering.stackexchange.com/q/36968011user7088941https://softwareengineering.stackexchange.com/users/3026252018-04-20T07:53:15Z2018-04-20T12:53:13Z
<p>I have a project that is sufficiently large in size that I can't keep every aspect in my head any more. I'm dealing with a number of classes and functions in it, and I'm passing data around.</p>
<p>With time I noticed that I kept getting errors, because I forgot what precise form the data has to have when I pass it to different functions (<em>e.g., one function accepts and outputs an array of strings, another function, that I wrote much later, accepts strings that are kept in a dictionary etc., so I have to transform the strings I'm working with from having them in an array to having them in a dictionary</em>).</p>
<p>To avoid always having to figure out what broke where, I started treating each function and class as being an "isolated entity" in the sense that it cannot rely on outside code giving it the correct input and has to perform input checks itself (or, in some cases, recast the data, if the data is given in the wrong form).</p>
<p>This has greatly reduced the time I spend making sure that the data that I pass around "fits" into every function, because the classes and functions themselves now warn me when some input is bad (and sometimes even correct that) and I don't have to go with a debugger through the whole code anymore to figure out where something went haywire.</p>
<p>One the other hand this has also increased the overall code.<br>
<strong>My question is, if this code style appropriate for solving this problem?</strong><br>
<strong>Of course, the best solution would be to completely refactor the project and make sure the data has a uniform structure for all functions - but since this project is growing constantly, I would end up spending more and worrying about clean code than actually adding new stuff.</strong></p>
<p>(FYI: I'm still a beginner, so please excuse if this question was naive; my project is in Python.)</p>