2

During my online class one of my python tutors told me that namedtuple can cause more harm than good.

I am confused why. Can someone please specify when to use namedtuple and when not to?

anothernode
  • 5,100
  • 13
  • 43
  • 62
  • 4
    It can make you become too great a programmer and put him out of a job – Mateen Ulhaq Jan 17 '18 at 09:10
  • https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python – Arpit Jan 17 '18 at 09:11
  • Possible duplicate of [What are "named tuples" in Python?](https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python) – anothernode Jan 17 '18 at 09:14
  • 1
    It adds great readibility to your code, and that's what python is for. Objectively, one could argue that `namedtuple` would be more memory consuming than a simple `tuple`. But if we wanted everything to be optimal to the max, we would all be programming websites in Assembly. But we are not because we have powerful machines nowadays. It **can cause** more harm than good **if-and-only-if** you absolutely need your program to be fast and optimized : in which case, you might not choose python at all. You can see a namedtuple as an immutable dict. – IMCoins Jan 17 '18 at 09:16
  • BY THE WAY, I shall encourage you to **ask** your teacher the question yourself next time. Good students are students that are not afraid to ask things they did not understand. You didn't understand why `namedtuple can cause more harm than good` ? Ask him to provide details. What if he can't ? He might be just as wrong as a student that will not provide details about his answer in his test. – IMCoins Jan 17 '18 at 09:34
  • @IMCoins I thought I am stupid to have such question. :) As no one asked – Riddhi Siddhi Jan 17 '18 at 09:37
  • “The only stupid question is the question that is never asked.” :) – IMCoins Jan 17 '18 at 09:39
  • I think the answer [here](https://softwareengineering.stackexchange.com/a/349742) is better than the ones posted. – Joe Apr 18 '23 at 13:48

2 Answers2

4

The classic example of namedtuple is something like this...

>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(x=1, y=2)
>>> p.x
1
p.y
2

The thing I think most people find attractive at first is the ability to make a class so easily and be able to instanciate with keyword args Point(x=1, y=2) and do dotted access like p.x and p.y.

However, there are a lot of things that are easily overlooked and is rather inflexible. Unexpected things can happen for subclasses, too. Unless namedtuple really hits your use-case, you're better off using SimpleNamespace if you just want dotted-name lookups and a nice repr.

from types import SimpleNamespace

class Point(SimpleNameSpace):
    def __init__(self, x, y=0):
        # simple wrapper to accept arguments positionally or as keywords with defaults
        super().__init__(x=x, y=y)
sytech
  • 29,298
  • 3
  • 45
  • 86
0

Few problem's what I can see is

You can't specify default arguments values for namedtuple classes.This makes them unwieldy when your data may have many optional properties .

The attribute values of namedtuple instances are still accessible using numerical indexes and iteration .Especially in externalised API's , this can lead to unintentional usage that makes it harder to move to real class later.If you are not in control of all of the usage of namedtuple instances , its' better define your own class

and for when to use it please do see the comment by IMCoins

Hariom Singh
  • 3,512
  • 6
  • 28
  • 52