Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python dataclass

from dataclasses import dataclass


@dataclass
class Rectangle:
    # Constructor
    height: float
    width: float


@dataclass
class Square(Rectangle):
    # initialization with default value
    side: float = 4

    # post initialize inheritance constructor
    def __post_init__(self):
        super().__init__(self.side, self.side)


sq = Square(height=10, width=2)
print(sq)
# output Square(height=4, width=4, side=4)
Comment

python dataclass

# Standard way of writing a simple class
class Person1:
# Type hinting not required
    def __init__(self, name: str, age: int, num_children=0):
        self.name = name
        self.age = age
        self.num_children = num_children

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

      
from dataclasses import dataclass


# A class using data classes. Dataclasses are simpler but can't support operations during initialization
@dataclass()
class Person2:
    """ This class handles the values related to a person. """
    name: str  # Indicating types is required
    age: int
    num_children = 0  # Default values don't require an indication of a type

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

# Both classes (Person1 and Person2) achieve the same thing but require different code to do it

person1 = Person1('Joe', 28, 2)
print(person1)
# Result: My name is Joe, I am 28 years old, and I have 2 children

person2 = Person2('Emma', 19)
print(person2)
# Result: My name is Emma, I am 19 years old, and I have 0 children
Comment

dataclass in python

# dataclass module is introduced in Python 3.7 as a utility tool
# to make structured classes specially for storing data.
# These classes hold certain properties and functions to deal
# specifically with the data and its representation.

pip install dataclasses

# Check other ans for the usage
Comment

PREVIOUS NEXT
Code Example
Python :: standard deviation in python without numpy 
Python :: how to plot a pandas dataframe with matplotlib 
Python :: pytest create server 
Python :: Send Fetch Request Django(Get Method) 
Python :: dimension of an indez pandas 
Python :: python use variable name as string 
Python :: Python NumPy array_split Function Syntax 
Python :: print string python 
Python :: * pattern by python 
Python :: block content 
Python :: python write column csv 
Python :: filter field set in django formds 
Python :: how to know the version of python 
Python :: functools.cached_property objects in python 
Python :: Python Renaming a Directory or a File 
Python :: recursion python examples 
Python :: python web app 
Python :: How to Get the length of all items in a list of lists in Python 
Python :: from random input python 
Python :: add a constant to a list python 
Python :: count occurrences of one variable grouped by another python 
Python :: Python NumPy Shape function syntax 
Python :: python loop 
Python :: django many to many post update method via rest 
Python :: random list generator 
Python :: a function to create a null matrix in python 
Python :: qr code scanner using opencv 
Python :: panda lambda function returns dataframe 
Python :: how to overlap two barplots in seaborn 
Python :: 3d array 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =