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).

92 questions with no upvoted or accepted answers
3 votes
1 answer
2k views

How to type hint a function that returns a NamedTuple that has been created within the function

My IDE's (PyCharm) linter indicates that I'm making a mistake with my return type-hinting when creating a namedtuple within a function. The problem appears to be that I'm saying that I will return ...
jedge's user avatar
  • 1,007
3 votes
0 answers
578 views

how to inherit from typing.NamedTuple and another class correctly?

There is a class I'm trying to extend, and to save on self.xxx = xxx boilerplate code I'm inheriting a typing.NamedTuple as well. I managed to make it work, but it looks really weird #!/usr/bin/env ...
Alexey Bashtanov's user avatar
3 votes
0 answers
93 views

How to document named tuple fields in Python?

When an API defines a named tuple, where would be the best place to document these fields? Ideally so MyType.__doc__ can be used to read back the information.
ideasman42's user avatar
  • 46.3k
3 votes
1 answer
981 views

Create namedtuple from Namspace?

How do I initialize a namedtuple from a Namespace? import collections import argparse nt=collections.namedtuple("nt",["foo","bar"]) # _NOT_ "baz"! parser = argparse.ArgumentParser() parser....
sds's user avatar
  • 59.5k
2 votes
0 answers
46 views

Any dict-like mappable objects in Python that only show preview of contents when __repr__?

I'm looking for an object I can use to store largish dataframes and sklearn objects. Ideally I would like to store them as pd.Series because it has the behavior I'm looking for in that I can do the ...
O.rka's user avatar
  • 30.5k
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
2 votes
0 answers
286 views

how to model simple nested data static structure instead of nested dictionaries (namedtuple, dataclass... )

I hope there is some best practice or design pattern for use case of having simple nested data/mapping with need to pre-store data, select proper data group, and conveniently access specific values. ...
stam's user avatar
  • 330
2 votes
1 answer
3k views

converting dict to typing.NamedTuple in python

For most, I am not sure if it's the right question to be asked but I couldn't yet found out why there are two different types of named tuple... I have read " What's the difference between namedtuple ...
kimiya_Ab's user avatar
2 votes
1 answer
362 views

What is the difference between namedtuple and 'type' function

I recently visited a forum with python tricks and came across this: >>> Points = type("Points", (object,), {'x' : None, 'y' : None}) >>> Player = Points() >>> Player.x = 23 ...
JuSt HumAn's user avatar
2 votes
2 answers
430 views

Passing Column Names from a tuple to Pandas

My Scenario looks like this, where i have identified the columns having NaN values using, nan_cols=tuple(train.columns[train.isnull().sum()>0]) Now, I need to find the correlation between these ...
Kavi Kumaran's user avatar
2 votes
1 answer
362 views

Python: How do I store and retrieve dynamic data in a namedtuple to write to a csv? Should I just use object attributes instead?

The goal is to write csv files for each Invoice from a webpage. I'm trying to do this with a webscraper, mainly using selenium Each Invoice has its own number, date, close_date, amount, and list of ...
Glen Cote's user avatar
2 votes
0 answers
85 views

Why doesn't namedtuple use super()?

I was browsing through the source code of namedtuple (see collections/__init__.py) to see how to inherit from tuple. I noticed in the __new__ method, it doesn't call super(): def __new__(_cls, {...
user avatar
2 votes
1 answer
1k views

What is the MATLAB equivalent of a named tuple in Python?

A namedtuple assigns names, as well as a numerical index, to each member, unlike the solution listed here. For example, what would be the MATLAB equivalent of the following in Python: myRow = ...
Aaron's user avatar
  • 132
1 vote
0 answers
36 views

Setting the return type of a custom namedtuple

I have a sample_config.json file with potentially an arbitrary number of the parameters. An example of the sample_config.json is as follows: { "tableA_ID": "tableA.csv", "...
user4687531's user avatar
  • 1,101
1 vote
1 answer
35 views

Why pyomo unpack namedtuples in constraints?

I am new to pyomo and I want to understand why it works with namedtuple the way it works. There is a code sample: from collections import namedtuple import pyomo.environ as pyo Product = namedtuple(&...
Alexey's user avatar
  • 99

15 30 50 per page
1
2 3 4 5
7