namedtuple

Difference between a class(namedtuple) and a namedtuple

Difference between a class(namedtuple) and a namedtuple Question: What is the difference between class(namedtuple) and only namedtuple, they look different but in both cases sensor seems to be the same thing, and what kind of functionality can be added inside class Sensor in case1? from collections import namedtuple # case 1 class Sensor(namedtuple(‘Sensor’, [‘name’, ‘location’, …

Total answers: 1

Using tuples and input functions

Using tuples and input functions Question: My task (for class): Write a program that takes a month number (like 3) and outputs the month name (like March). Use a list or tuple to store all of the month names and then use the month number to index. Example: >>> Enter a month number from 1 …

Total answers: 2

Why can we inherit `typing.NamedTuple`?

Why can we inherit `typing.NamedTuple`? Question: 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 collections.namedtuple, this syntax is more beautiful, but I still can’t understand its implementation, whether we look at typing.py file, or do …

Total answers: 1

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

How to add a list of field names to a class based on NamedTuple? Question: 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 to use in Class A, essentially: more_field_names …

Total answers: 3

Are pickle-able tuple-factories (with names) possible?

Are pickle-able tuple-factories (with names) possible? Question: There are several questions about pickling namedtuples already, however none of the ones I found [1] [2] [3] [4] deals with the case of pickling a namedtuple that is bound on an object instance. Consider the following example import pickle from collections import namedtuple class TupleSplitter: r"""Splits a …

Total answers: 2

namedtuple._source not working in python 3.7

namedtuple._source not working in python 3.7 Question: I’ve tried with this code from collections import namedtuple t = namedtuple(‘t’, ‘a b c’) i = t(1,2,3) print(i._source, t._source) But when I run it it says that there is no attribute _source (for t and therefore also for i). Has it been eliminated since 3.6? Asked By: …

Total answers: 1

Data Classes vs typing.NamedTuple primary use cases

Data Classes vs typing.NamedTuple primary use cases Question: Long story short PEP-557 introduced data classes into Python standard library, that basically can fill the same role as collections.namedtuple and typing.NamedTuple. And now I’m wondering how to separate the use cases in which namedtuple is still a better solution. Data classes advantages over NamedTuple Of course, …

Total answers: 6

Python mutable NamedTuple

Python mutable NamedTuple Question: I am looking for a struct like data structure I can create multiple instances from and have some type hinting without being immutable. So I have something like this: class ConnectionConfig(NamedTuple): name: str url: str port: int user: str = “” pwd: str = “” client: Any = None But I …

Total answers: 6

How to access a field of a namedtuple using a variable for the field name?

How to access a field of a namedtuple using a variable for the field name? Question: I can access elements of a named tuple by name as follows(*): from collections import namedtuple Car = namedtuple(‘Car’, ‘color mileage’) my_car = Car(‘red’, 100) print my_car.color But how can I use a variable to specify the name of …

Total answers: 5

Pythonic way to convert a dictionary into namedtuple or another hashable dict-like?

Pythonic way to convert a dictionary into namedtuple or another hashable dict-like? Question: I have a dictionary like: d = {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4} which I would like to convert to a namedtuple. My current approach is with the following code namedTupleConstructor = namedtuple(‘myNamedTuple’, ‘ ‘.join(sorted(d.keys()))) nt= namedTupleConstructor(**d) which produces myNamedTuple(a=1, …

Total answers: 10