Questions tagged [namedtuple]

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

Documentation: collections module in Python 3.

Recently, a declarative API for namedtuples is also provided by the typing module.

545 questions
0
votes
0 answers

Is it possible to implement methods in subclass of typing.NamedTuple? And, are properties considered methods?

I did this to represent a person using typing.NamedTuple: from typing import NamedTuple class Phone(NamedTuple): residential: str mobile: str class Person(NamedTuple): name: str last_name: str phone: Phone @property …
eddyxide
  • 13
  • 4
0
votes
2 answers

NamedTuple is shared across variables

from typing import NamedTuple, List, Set, Tuple, Dict class EmbeddingInfoStruct(NamedTuple): emb_names : list[str] =[] idx_in_data: list[int] =[] emb_dim: list[int] =[] info1…
imachabeli
  • 66
  • 8
0
votes
1 answer

Python data json dictionary

I wanted to read tar.json file, so I write: import json with open('tar.json', "r", encoding='utf-8') as read_file: data = json.load(read_file) and print it as dictionary, for which the key will be "linia" (written as int) and the value…
Malum Phobos
  • 115
  • 6
0
votes
1 answer

How to access name of named tuple in Typescript

I want to access the name from a named tuple to use within one of my functions. Is there a way to access the name only, similar to how you can access the type only by using the index of the tuple element. type NamedThing = [emailId: string,…
sayandcode
  • 1,775
  • 1
  • 11
  • 24
0
votes
0 answers

Named Tuple workaroung for change Value in python

in my project i need change namedTuple variable's value, but it's impossible because is a "Tuple" and they are immutable. But i want overwrite setAttr function for use _replace function of namedTuple. from collections import…
Catanzaro
  • 1,312
  • 2
  • 13
  • 24
0
votes
0 answers

how to refer to a class that was imported from a different file in python for unit testing?

so i have this method here inside a class Foo Position = namedTuple('Position', ['row', 'column']) class Foo: ... def hasObject(self, position:Position)->bool: i'm trying to write a unit test for this method and so far it looks like…
0
votes
0 answers

Dynamically extend `__del__` cleanup code on an instance

I want to bind a dynamically created namedtuple to a class When an instance of the class is initialized, a new namedtuple-type is created, and registered in globals() (this allows pickling). When the instance is deleted, I want to perform cleanup…
Hyperplane
  • 1,422
  • 1
  • 14
  • 28
0
votes
1 answer

Use Tuple values in Object initalizer syntax

I'm trying to initialize properties of an object I am creating with the values of a named tuple. Something like this public Person DoIt() { return new Person { (First, Last) = GetFirstAndLast(id) }; } public (string first, string last)…
comecme
  • 6,086
  • 10
  • 39
  • 67
0
votes
0 answers

How to check type of namedtuple defined within a different scope

If a namedtuple is defined within a function, is it possible to use isinstance() to check the type of the namedtuple outside of the function. I'm surprised that I'm able to use type() to get the type, but the isinstance() function…
0
votes
0 answers

types.GenericAlias - subscriptable Types and their use

I was working with namedtuples and learned something that I found odd at first. Still not quite sure how or why this works and what can be the use of this: from collections import namedtuple Card = namedtuple('Card', ['rank', 'suit']) Above code…
pangna
  • 61
  • 4
0
votes
0 answers

Python serialize namedtuple key in dictionary to json

FB = namedtuple(“FB”, (“foo”,”bar”)) fb = FB(123, 456) test_dict={fb:999} How to serialize to json for test_dict?
ideate
  • 1
0
votes
1 answer

Python namedtuple: AttributeError: 'tuple' object has no attribute 'end_pos'

I have a class that starts as follows: from collections import namedtuple class Parser: Rule = namedtuple('Rule', ['lhs', 'rhs', 'dot_pos', 'start_pos', 'end_pos']) # __init__ ... Since PyCharm detected all my tuple element namings…
TiMauzi
  • 190
  • 1
  • 3
  • 16
0
votes
2 answers

Getting error when trying to create namedtuple object using _make function

Hi all I recently started learning Python collections module. When I was trying to implement namedtuple collections for practice I got an error. I tried searching for in the official Python documentation but could not find. Can you all please help…
0
votes
0 answers

Why is namedtuple rename=True renaming an "_z" field

So "_z" is not a keyword, and is a value identifier. print("_z".isidentifier()) returns True import keyword print(keyword.iskeyword("_z")) returns False So why is named tuple renaming the column? Point3D = namedtuple('Point3D', ['x','y','_z'],…
smackenzie
  • 2,880
  • 7
  • 46
  • 99
0
votes
1 answer

Storing value for a point using named tuple?

I am trying to design a spreadsheet app. I have been able to come up with a class for my Index which is typically the point consisting of row, col. I want to be able to assign a value to the point. The challenge is I am unable to update the value…
SDRJ
  • 532
  • 3
  • 11