Difference between DataClass vs NamedTuple vs Object in Python - GeeksforGeeks

01 Aug, 2020
Data Class: Data Class is a type of class that is used to store data without any functionality. These data classes are just regular classes having the main purpose to store state and data without knowing the constraints and logic behind it. Whenever you create a class that mostly contains attributes and certain properties to deal with the data and its representation.
Example:
Python3
# Importing dataclass module from dataclasses import dataclass   # Annotation@dataclass  # Class with attributesclass 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)
Output:
GfgArticle(topic=’DataClasses’, contributor=’nightfury1’, language=’Python’, upvotes=1)
NamedTuple: The NamedTuple is a class that contains the data like a dictionary format stored under the ‘collections‘ module. It stored the data in a key-value format where each key having mapped to more values. So, we access those data using a specific key or can use the indexes.
Example:
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'))
Output:
The Article Topic : Difference between DataClass vs NamedTuple vs Object in PythonThe Article Contributor Name : night_fury1The Article Contributor Post : Technical Content Writer Intern
Object: An object is simply a collection of data (variables) and methods (functions) that act on those data. In other words, we say that Object is an instance of the class.
Example:
Python3
# Python script for demostraation of objectclass 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 callg.myfunc()
Output:
Article:  Difference between DataClass vs NamedTuple vs Object in PythonContributor:  nightfury1
Table of difference between DataClass, NamedTuple and Object
DataClass
NamedTuple
Object
Python collections-module
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python Classes and Objects