namedtuple

Is there a dictionary-like object that is immutable? [duplicate]

强颜欢笑 提交于 2020-06-23 14:16:05
问题 This question already has answers here : What would a “frozen dict” be? (14 answers) Closed 12 days ago . I would like a Python object that can flexibly take any key and I can access by key, like a dictionary, but is immutable. One option could be to flexibly generate a namedtuple but is it bad practice to do this? In the example below a linter would not expect nt to have attribute a for example. Example: from collections import namedtuple def foo(bar): MyNamedTuple = namedtuple("MyNamedTuple

Weird MRO result when inheriting directly from typing.NamedTuple

久未见 提交于 2020-05-29 04:07:27
问题 I am confused why FooBar.__mro__ doesn't show <class '__main__.Parent'> like the above two. I still don't know why after some digging into the CPython source code. from typing import NamedTuple from collections import namedtuple A = namedtuple('A', ['test']) class B(NamedTuple): test: str class Parent: pass class Foo(Parent, A): pass class Bar(Parent, B): pass class FooBar(Parent, NamedTuple): pass print(Foo.__mro__) # prints (<class '__main__.Foo'>, <class '__main__.Parent'>, <class '__main_

How to apply a special methods 'Mixin' to a typing.NamedTuple

♀尐吖头ヾ 提交于 2020-05-13 02:35:18
问题 I love the typing.NamedTuple in Python 3.6. But there's often the case where the namedtuple contains a non-hashable attribute and I want to use it as a dict key or set member. If it makes sense that a namedtuple class uses object identity ( id() for __eq__ and __hash__ ) then adding those methods to the class works fine. However, I now have this pattern in my code in several places and I want to get rid of the boilerplate __eq__ and __hash__ method definitions. I know namedtuple 's are not

How to apply a special methods 'Mixin' to a typing.NamedTuple

蹲街弑〆低调 提交于 2020-05-13 02:28:47
问题 I love the typing.NamedTuple in Python 3.6. But there's often the case where the namedtuple contains a non-hashable attribute and I want to use it as a dict key or set member. If it makes sense that a namedtuple class uses object identity ( id() for __eq__ and __hash__ ) then adding those methods to the class works fine. However, I now have this pattern in my code in several places and I want to get rid of the boilerplate __eq__ and __hash__ method definitions. I know namedtuple 's are not

Data Classes vs typing.NamedTuple primary use cases

微笑、不失礼 提交于 2020-02-17 08:57:22
问题 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, all the credit goes to dataclass if we need: mutable objects inheritance support property decorators, manageable attributes generated method definitions out of the box or

Data Classes vs typing.NamedTuple primary use cases

痴心易碎 提交于 2020-02-17 08:56:14
问题 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, all the credit goes to dataclass if we need: mutable objects inheritance support property decorators, manageable attributes generated method definitions out of the box or

Is there a use for _tuple in Python?

若如初见. 提交于 2020-02-03 05:29:05
问题 I read the official documentation of collections.namedtuple today and found _tuple in the __new__ method. I did not find where the _tuple was defined. You can try running the code below in Python, it does not raise any error. >>> Point = namedtuple('Point', ['x', 'y'], verbose=True) class Point(tuple): 'Point(x, y)' __slots__ = () _fields = ('x', 'y') def __new__(_cls, x, y): 'Create a new instance of Point(x, y)' return _tuple.__new__(_cls, (x, y)) # Here. Why _tuple? Update: What are the

Converting values of named tuples from strings to integers

天大地大妈咪最大 提交于 2020-01-02 20:58:32
问题 I'm creating a script to read a csv file into a set of named tuples from their column headers. I will then use these namedtuples to pull out rows of data which meet certain criteria. I've worked out the input (shown below), but am having issues with filtering the data before outputting it to another file. import csv from collections import namedtuple with open('test_data.csv') as f: f_csv = csv.reader(f) #read using csv.reader() Base = namedtuple('Base', next(f_csv)) #create namedtuple keys

What is a nicer alternative to a namedtuples _replace?

白昼怎懂夜的黑 提交于 2020-01-02 02:28:07
问题 Take this code: >>> import urlparse >>> parts = urlparse.urlparse('http://docs.python.org/library/') >>> parts = parts._replace(path='/3.0'+parts.path) parts._replace works but as it is an underscored method, it's supposed to be internal, and not used. Is there an alternative? I don't want to do: >>> parts = parts[:2] + ('/3.0'+parts.path,) + parts[3:] Because that makes it an ordinary tuple, and not a namedtuple, and doing: >>> parts = namedtuple(scheme=parts.scheme, netloc=parts.netloc, etc

create custom namedtuple type with extra features

Deadly 提交于 2019-12-30 19:14:29
问题 I'd like to create my own type of build-in namedtuple that has some extra features. Let's say we create a class: from collections import namedtuple MyClass = namedtuple('MyClass', 'field1 field2') It`s immutable, readable and simple. Now I can create instances of MyClass: myobj = MyClass(field1 = 1, field2 = 3.0) print(myobj.field1, myobj.field2) My extra requirement is when instance is created I'd like to check if field1 is int type and field2 is float . For example if user try to create