Skip to main content

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
0 votes
0 answers
79 views

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, ...
dewser_the_board's user avatar
5 votes
1 answer
914 views

Why can we inherit `typing.NamedTuple`?

After Python 3.6, we have typing.NamedTuple, which is a typed version of collections.namedtuple(), we can inherit it like a class: class Employee(NamedTuple): name: str id: int Compared with ...
Mechanic Pig's user avatar
  • 7,716
1 vote
3 answers
449 views

How to add a list of field names to a class based on NamedTuple?

I have a basic class like this: from typing import NamedTuple class A(NamedTuple): f1: str = "" aa = A("haha") print(aa) Now suppose I have a list of more fields that I want ...
user3768495's user avatar
  • 4,447
0 votes
0 answers
386 views

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 = ...
pangna's user avatar
  • 61
1 vote
3 answers
203 views

Python namedtuples elementwise addition

Is there a more pythonic way to implement elementwise addition for named tuples? Using this class that inherits from a namedtuple generated class named "Point I can do elementwise addition for ...
Isaacnfairplay's user avatar
2 votes
0 answers
42 views

Concise creation of an object with an attribute in Python

I want to pass an object with attribute key set to 42 to function f. This will be done only once, and the object and its type is discarded. Here is my solution: from collections import namedtuple def ...
Paul Jurczak's user avatar
  • 7,802
0 votes
0 answers
20 views

Python serialize namedtuple key in dictionary to json [duplicate]

FB = namedtuple(“FB”, (“foo”,”bar”)) fb = FB(123, 456) test_dict={fb:999} How to serialize to json for test_dict?
ideate's user avatar
  • 1
1 vote
1 answer
803 views

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 ...
TiMauzi's user avatar
  • 236
0 votes
2 answers
164 views

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 ...
Vishal Lade's user avatar
0 votes
0 answers
22 views

Why is namedtuple rename=True renaming an "_z" field [duplicate]

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 ...
smackenzie's user avatar
  • 2,982
4 votes
2 answers
318 views

Map a function to all a typing.NamedTuple fields and get one back?

Is there a way to apply a function to all entries of a NamedTuple and get an object of the same type back? The following code from typing import NamedTuple class MyNamedTuple(NamedTuple): a: int ...
fuenfundachtzig's user avatar
2 votes
2 answers
215 views

Not able to add value to List of named tuple

I have a named List of named tuple and from this function OnEndCircularReferencesCalculation() I am trying to add value to named tuple but getting error message like : 'Index was out of range. Must be ...
Ramesh Dutta's user avatar
0 votes
1 answer
54 views

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. ...
SDRJ's user avatar
  • 540
4 votes
1 answer
2k views

Apache Beam infer schema using NamedTuple (Python)

I am quite new to apache beam and I am wondering how to infer schema to a pcollection using namedtuple. The example from the documentation Programming Guide states: class Transaction(typing.NamedTuple)...
Sephixx's user avatar
  • 157
-2 votes
1 answer
197 views

Write a new CSV File from Existing CSV File with many Conditions

I want to write a CSV File which gets Data from and depending from an existing CSV File. Existing_FILE: Date, Value, Name, sh 2022-01-15, 30,00, Monthly1 - #1500, H 2022-01-18, 130,00, Monthly50, S ...
RichardeRicharde's user avatar

15 30 50 per page
1 2
3
4 5
37