简体   繁体   English

您可以使用 namedtuples/dataclass/attrs 创建灵活大小的 class 还是必须使用传统类?

[英]Can you use namedtuples/dataclass/attrs to create a flexible sized class or do you have to use traditional classes?

If there is a json data set that looks like this.如果有一个看起来像这样的 json 数据集。

[
{'a':1,'b':'fire','c':'cambodia','type':'charizard'},
{'a':2,'d':'waterparks','type':'squirtle'},
{'a':3,'f':'thunder','type':'pikachu'}
]

And it is needed to transition it into a set of objects where the objects can be defined with the same class like this.并且需要将其转换为一组对象,其中对象可以像这样使用相同的 class 定义。

charizard = Pokemon(row_data)
pickachu = Pokemon(row_data)
squirtle = Pokemon(row_data)

But the attributes are accessible via dot notation, like this.但是属性可以通过点符号访问,就像这样。

charizard.a
pikachu.d
squirtle.a

The way to do that with traditional classes is like this.使用传统课程的方法是这样的。

class Pokemon(object):
    def __init__(self, data):
        for k, v in data.items():
            setattr(self, k, v)

Is there a way to do basically the same thing with either namedtuples or dataclasses or attrs that works for data of different sizes and has all the nice immutability, repr, etc. functionality of those data types.有没有办法对命名元组或数据类或属性做基本相同的事情,适用于不同大小的数据,并具有这些数据类型的所有良好的不变性、repr 等功能。

The point of attrs et al is to have well-defined classes for your data. attrs 等人的重点是为您的数据定义明确的类。 So the short answer to your question is “no”.所以你的问题的简短回答是“不”。

Setting attributes dynamically like you you want to doesn't buy you much, except that it's less typing to access your data.像你想要的那样动态地设置属性不会给你带来太多好处,除了它可以减少输入来访问你的数据。

A better way is to have well-defined classes where one look tells you what attributes to expect and serialize/normalize your JSON into it.一种更好的方法是拥有定义明确的类,其中一看就会告诉您期望哪些属性并将您的 JSON 序列化/规范化到其中。 There's a bunch of packages for that;有一堆包。 for attrs there's cattrs for instance.例如 attrs 有cattrs Or you just write a function or class method.或者你只写一个 function 或 class 方法。

The time you spend making this transition explicit, you'll get back tenfold when debugging later.你花在明确这个转换上的时间,你会在以后调试时得到十倍的回报。

Use attrs for data with schema.将 attrs 用于具有模式的数据。 If your data DOES have a schema (even if it includes a whole lot of attributes), yes you can use attrs and you can even define data schema dynamically.如果您的数据确实有一个模式(即使它包含很多属性),是的,您可以使用 attrs,您甚至可以动态定义数据模式。 But if your data is schemaless, don't define schema on it.但是,如果您的数据是无模式的,请不要在其上定义模式。

If you can give a few real examples (at least 10 of them), maybe that will help us understand your exact situation.如果你能举几个真实的例子(至少10个),也许这将有助于我们了解你的确切情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可以在 python 数据类中使用工厂吗? - Can you use factories in a python dataclass? 如何在字典中使用冻结的数据类并将其导出到 YAML? - How do you use a frozen dataclass in a dictionary and export it to YAML? 你能创建一个类对象列表并以这种方式使用类函数吗? - Can you create a list of class objects and use class functions that way? 是否可以在 python 数据类中创建联合字段? (pydantic,数据类,属性) - Is it possible to create union fields in python data classes? (pydantic, dataclass, attrs) 你可以在Python中使用字符串格式吗? - Can you use string formatting for classes in Python? 你能在Python中创建传统的固定长度和类型数组吗? - Can you create traditional fixed length and type arrays in Python? 你能用字符串实例化一个类吗? - Can you use a string to instantiate a class? 你可以使用 if 语句来创建变量吗? - Can you use if statements to create variables? 我可以使用 xsdata python 库(或您知道的更好的选择)将 python 数据对象(使用数据类)序列化为使用 xsd 的 xml 输出吗? - Can I use xsdata python library (or a better option that you know) to serialize a python data object(using dataclass) to an xml output using a xsd? 如何使用Beautiful Soup提取具有某些类属性的列表项? - How do you use Beautiful Soup to pull out list items that have certain class attributes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM