Python: About Dataclass and How to Initialize Dataclasses Object With Function that Returns Multiple Objects

Won Woo William Lyu·2022년 4월 1일
0

Python

목록 보기
1/1
post-thumbnail

What is Dataclasses in Python?

Dataclasses is a new feature that is introduced in Python 3.7, which allows python code to be more compact than before.

THis was possible, because it automatically initialize special methods of __init__, __repr__ and __eq__ for users.

Here is an example that compares between original class and dataclasses with a car.

class Car: #original class initialization
    
    def __init__(self, name, brand, year):
		self.name = name
        self.brand = brand
        self.year = year
    
    def __repr__(self):
    	return f"{self.__class__.__name__} : {self.name!r}, {self.brand!r}, {self.year!r}"
    
    def __eq__(self, other):
    	if other.__class__ is not self.__class__:
        	return "Wrong Inputs. Check Classes"
        else:
        	return (self.name, self.brand, self.year) == (self.)
-------------------------------------------------------------------------------
 
 small tips: `!r` format will print only object part inside single quote
 car = Car("Sportage", "KIA", "2021")
 print car without !r → Car : Sportage, KIA, 2021
 print car with !r → Car : 'Sportage', 'KIA', '2021'

Originally, python classes needed to be written like above one. Let's check how it will be look like with using dataclasses.

from dataclasses import dataclass

@dataclass
class Car:
	name : str
   	brand : str
   	year : str

Above code is pretty much it since dataclasses automatically creates __repr__ and __eq__ for users.

Both of original and dataclasses have same way of initializing, and below is a code that how output will look like if you try to print dataclass object.

 car = Car("Sportage", "KIA", "2021")
 
 print(car)
 print(car.name)
 print(car == Car("Sportage", "KIA", "2021"))
 
 OUTPUT:
 Car(name='Sportage', brand='KIA', year='2021')
 Sportage
 True

Main: How to Initialize Dataclass Object With Function that Returns Multiple Objects.

We have discusssed breif about what is dataclasses in above section.

Now we will talk about the main point of this article.

How can we initialize Dataclass Object with Function that Returns Multiple Objects?

This situation does not come frequently, because usually you just return one variable of list or tuple that has multiple objects.

However, sometimes there can be time when the function's input and output are fixed and you want to easily make an dataclass object with this output.

Here is an easy way of making dataclasses object with function that returns multiple values.

# Fixed Input, Output Function
def arithmetic(x, y):
	addition = x + y
    subtraction = x - y
    multipliation = x + y
    division = x / y
	
    return addition, subtraction, multiplication, division
from dataclasses import dataclass

@dataclass
class Output:
	add : float
    sub : float
    mul : float
    div : float

Let's say that there is a function arithmetic that returns 4 values of simple calculations.

Initializing Output class with arithmetic function results can be done in two line like below code

add, sub, mul, div = arithmetic(10, 5)
output = Output(add, sub, mul, div)

This already looks simple, and can think how can this be a problem.

Well, having four outputs are fine, but what if there are about more than 10 output?
You have to make variable for each of them and recall each of them when initializing on dataclasses input.

This inconvenient way can be solved by using a * in python to directly put output of function to input of dataclass like below code

output = Output( *(arithmetic(10, 5)) )

What above code doing is that Output class is initialized with unpacked tuple that has arithmetic function outputs.

To understand this, you first have to understand unpacking operator, Asterisk *.

Using *, you can unpack tuple into element. Below code will make easy to understand

print(arithmetic(10, 5))
print(*(arithmetic(10, 5)))

OUTPUT:
(15.0, 5.0, 50.0, 2.0)
15.0 5.0 50.0 2.0

By making outputs into tuple with () and unpack it with *, each element was able to assign to each argument and was able to easily initialize dataclass object.

Personally, I think dataclass makes python to be more like a java style initialization

If you are familiar with java and need to code with python? Please try this library! You won't regret it.

profile
Hello!! I am an ENFP Backend Engineer!!

0개의 댓글