namedtuple

Inheriting from a namedtuple base class

Inheriting from a namedtuple base class Question: This question is asking the opposite of Inherit namedtuple from a base class in python , where the aim is to inherit a subclass from a namedtuple and not vice versa. In normal inheritance, this works: class Y(object): def __init__(self, a, b, c): self.a = a self.b = …

Total answers: 4

What's the difference between enum and namedtuple?

What's the difference between enum and namedtuple? Question: I would like to know what are the differences between enum and namedtuple and when one should use one over the other. Asked By: Carlos Afonso || Source Answers: As an analogy (albeit an imperfect one), you can think of enum.Enum and namedtuple in python as enum …

Total answers: 2

Python syntax for namedtuple inside a namedtuple

Python syntax for namedtuple inside a namedtuple Question: Is it possible to have a namedtuple inside another namedtuple? For example: from collections import namedtuple Position = namedtuple(‘Position’, ‘x y’) Token = namedtuple(‘Token’, [‘key’, ‘value’, Position]) which gives a “ValueError: Type names and field names must be valid identifiers” Also, I am curious if there is …

Total answers: 2

Type hints in namedtuple

Type hints in namedtuple Question: Consider following piece of code: from collections import namedtuple point = namedtuple(“Point”, (“x:int”, “y:int”)) The Code above is just a way to demonstrate as to what I am trying to achieve. I would like to make namedtuple with type hints. Do you know any elegant way how to achieve result …

Total answers: 3

Did something about `namedtuple` change in 3.5.1?

Did something about `namedtuple` change in 3.5.1? Question: On Python 3.5.0: >>> from collections import namedtuple >>> cluster = namedtuple(‘Cluster’, [‘a’, ‘b’]) >>> c = cluster(a=4, b=9) >>> c Cluster(a=4, b=9) >>> vars(c) OrderedDict([(‘a’, 4), (‘b’, 9)]) On Python 3.5.1: >>> from collections import namedtuple >>> cluster = namedtuple(‘Cluster’, [‘a’, ‘b’]) >>> c = cluster(a=4, …

Total answers: 3

Mutable default argument for a Python namedtuple

Mutable default argument for a Python namedtuple Question: I came across a neat way of having namedtuples use default arguments from here. from collections import namedtuple Node = namedtuple(‘Node’, ‘val left right’) Node.__new__.__defaults__ = (None, None, None) Node() Node(val=None, left=None, right=None) What would you do if you would want the default value for ‘right’ to …

Total answers: 4

Looping over elements of named tuple in python

Looping over elements of named tuple in python Question: I have a named tuple which I assign values to like this: class test(object): self.CFTs = collections.namedtuple(‘CFTs’, ‘c4annual c4perren c3perren ntfixing’) self.CFTs.c4annual = numpy.zeros(shape=(self.yshape, self.xshape)) self.CFTs.c4perren = numpy.zeros(shape=(self.yshape, self.xshape)) self.CFTs.c3perren = numpy.zeros(shape=(self.yshape, self.xshape)) self.CFTs.ntfixing = numpy.zeros(shape=(self.yshape, self.xshape)) Is there a way to loop over elements of …

Total answers: 3

Existence of mutable named tuple in Python?

Existence of mutable named tuple in Python? Question: Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Camelot import namedgroup Point = namedgroup(‘Point’, [‘x’, ‘y’]) p = Point(0, 0) p.x = 10 >>> p …

Total answers: 14

Why doesn't the namedtuple module use a metaclass to create nt class objects?

Why doesn't the namedtuple module use a metaclass to create nt class objects? Question: I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string. Then …

Total answers: 4