📜  Python中DataClass vs NamedTuple vs Object之间的区别

📅  最后修改于: 2022-05-13 01:54:35.725000             🧑  作者: Mango

Python中DataClass vs NamedTuple vs Object之间的区别

数据类:数据类是一种用于存储数据的类,没有任何功能。这些数据类只是常规类,其主要目的是存储状态和数据,而不知道其背后的约束和逻辑。每当您创建一个主要包含属性和某些属性来处理数据及其表示的类时。

例子:

Python3
# Importing dataclass module 
from dataclasses import dataclass 
  
# Annotation
@dataclass
  
# Class with attributes
class GeeksArticle(): 
    """A class for holding an article content"""
    
    # Attributes Declaration 
    # using Type Hints 
    topic: str
    contributor: str
    language: str
    upvotes: int
    
# A DataClass object 
article = GeeksArticle("DataClasses", "nightfury1", "Python", 1)
  
print(article)


Python3
# Python script to demonstrate namedtuple()
    
# importing nametuple() from collections module 
from collections import namedtuple
    
# Declaring namedtuple() 
Contributor = namedtuple('Contributor', ['topic', 'author', 'post']) 
    
# Adding values 
C = Contributor('Difference between DataClass vs NamedTuple vs Object in Python',
                'night_fury1',
                'Technical Content Writer Intern') 
    
# Access using index 
print ("The Article Topic : ", end ="") 
print (C[0]) 
    
# Access using name  
print ("The Article Contributor Name : ", end ="") 
print (C.author) 
    
# Access using getattr() 
print ("The Article Contributor Post : ", end ="") 
print (getattr(C, 'post'))


Python3
# Python script for demostraation of object
class Gfg:
  def __init__(self, topic, contributor):
    self.topic = topic
    self.contributor = contributor
  
  def myfunc(self):
    print("Article: ", self.topic)
    print("Contributor: ", self.contributor)
  
# objects creation    
g = Gfg("Difference between DataClass vs NamedTuple vs Object in Python", 
        "nightfury1")
  
# function call
g.myfunc()


输出:

命名元组: NamedTuple 是一个包含数据的类,类似于存储在“集合”模块下的字典格式。它以键值格式存储数据,其中每个键都映射到更多值。因此,我们使用特定的键或可以使用索引来访问这些数据。

例子:

Python3

# Python script to demonstrate namedtuple()
    
# importing nametuple() from collections module 
from collections import namedtuple
    
# Declaring namedtuple() 
Contributor = namedtuple('Contributor', ['topic', 'author', 'post']) 
    
# Adding values 
C = Contributor('Difference between DataClass vs NamedTuple vs Object in Python',
                'night_fury1',
                'Technical Content Writer Intern') 
    
# Access using index 
print ("The Article Topic : ", end ="") 
print (C[0]) 
    
# Access using name  
print ("The Article Contributor Name : ", end ="") 
print (C.author) 
    
# Access using getattr() 
print ("The Article Contributor Post : ", end ="") 
print (getattr(C, 'post'))

输出:

目的: 对象只是数据(变量)和作用于这些数据的方法(函数)的集合。换句话说,我们说 Object 是类的一个实例。

例子:

Python3

# Python script for demostraation of object
class Gfg:
  def __init__(self, topic, contributor):
    self.topic = topic
    self.contributor = contributor
  
  def myfunc(self):
    print("Article: ", self.topic)
    print("Contributor: ", self.contributor)
  
# objects creation    
g = Gfg("Difference between DataClass vs NamedTuple vs Object in Python", 
        "nightfury1")
  
# function call
g.myfunc()

输出:

DataClass、NamedTuple 和 Object 的区别表

 

DataClass

NamedTuple

Object

CreationDataClass is slower than others while creating data objects (2.94 µs).NamedTuple is the faster one while creating data objects (2.01 µs).An object is slower than DataClass but faster than NamedTuple while creating data objects (2.34 µs).
Read PropertyEqual to Object & faster than NamedTuple while reading the data objects (24.7 ns).NamedTuple is slower than others in reading data objects (26.9 ns).Equal to DataClass & Faster than NamedTuple (24.7 ns).
Nested PropertyFaster than others while nesting data objects & their properties (48.1 ns).Slower than others in nesting data objects (75.8 ns).Faster than NamedTuple but slower than DataClass (52.1 ns).
Execute FunctionFaster than NamedTuple but slower than objects (829 ns).Slower than others in Function Execution (946 ns).Fastest in Function execution on data objects (821 ns).
Size56 bytes80 bytes56 bytes