Join us

Data Classes in python 😈(part-2)

In this Blog, Let’s discuss dataclass decorator parameters. We can control the features of the dataclass by passing arguments to dataclass decorator .

Prerequisite : Please Read Part-1(Hyper Link)

In this Blog, Let’s discuss dataclass decorator parameters. We can control the features of the dataclass by passing arguments to dataclass decorator . The syntax of the dataclass decorator is :

@dataclasses.dataclass(*,init=True, repr=True, eq=True, order=False,unsafe_hash=False, frozen=False,match_args=True, kw_only=True, slots=False)

What is this bro ? Looks so complex ! 😕. I’ll make you guys understand easily. Out of these 9 parameters , 6 are important . Let’s focus 3 in this blog and rest of the 3 in the upcoming blog (Data Classes in python (part-3)).


Let’s discuss internal __init__ or default constructor

init = True, means Dataclass uses internal constructor to initialize the defined data by the user . This is by default in data classes. If i make init = False, I can no longer initialize values for my object.

Yes, It raises TypeError. So, I handled using Exceptions Handling.

output of the above code :

As, We did init = False in dataclass decorator, python raises error that Dataclass won’t take any arguments.

Now, __repr__ won’t work . Why ?

__repr__ relies on internal __init__ . As long as internal __init__ won’t take any data for initializing , __repr__ will not allowed to print the objects associated data.

You may get a doubt as , What if i create my own constructor when data class is in init = False mode ? Do the internal __repr__ works now ?

Yes internal __repr__ works when i create my own __init__ but , the data will not be accepted by internal __repr__ as internal __repr__ is only bounded to the internal __init__ of a data class. Look below to understand more deeply…

Can you guess what the last line prints ?

It prints “FavFood()” because internal __init__ is in ‘False’ mode. So __repr__will not consider external __init__ we created .

output of the above code :


Let’s discuss what happens when internal __repr__ in False mode

In the above code __repr__ in ‘False’ mode . So when i print the object , i won’t return the user understandable string with the objects data in it .

Let’s see what i will get !

The output with __repr__ in ‘False’ mode is not understandable right ? 😅So, Never use __repr__ in ‘False’ mode.


Let’s discuss the last topic in this blog . i.e, “eq” parameter

In the first blog on data classes i said that “2 or more data class objects with same data will always evaluate to true on == operator”. If i make eq = False ,then this property will no longer applicable to data class objects.

Generally we will get ‘True’ for the above code output. As, eq = False , data class decorator will no longer compare 2 objects by taking their associated data in them. But, the 2 objects are compared based on their hash value.


The remaining 3 important parameters will be discussed in the coming blog (Data Classes in python (part-3)).

Follow, clap and comment if you like this blog.


Will post Data classes in python (part-3) soon……..


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

yokesh Chowdary

Backend developer, Clarivate

@yokee-ss-hh
Loves Backend web development, Consistently playing with Datastructures and algorithms.
User Popularity
24

Influence

2k

Total Hits

3

Posts