Akshay Kaushik πŸπŸ‘¨β€πŸ’» Profile picture
Feb 5, 2022 β€’ 9 tweets β€’ 4 min read β€’ Read on X
Do you know #Python "Data Classes"

They are a great Python feature that can help you write less code

🧡Let's find out moreπŸ‘‡
1️⃣ dataclass decorator adds generated methods
- __init__
- __repr__
- __eq__

to the class and returns the same class it was given. This module provides a deco...
2️⃣ Generated __init__ method takes all fields as function parameters. Their values are set to instance attributes with the same names.

Fields are defined as type annotated class attributes.

For example, you can define User with fields id and name.πŸ‘‡ A dataclass is a Python mod...
3️⃣ Generated __repr__ method returns a string containing:

- class name
- field names
- field representation

The order of fields is the same as the order of their definition in a class. πŸ‘‡ A Python class is a program...
4️⃣ Generated __eq__ method compares the class tuples containing field values of the current and the other instance.

It supports only instances of the same class

True is returned if:
- current and other are of the same class
- fields of both have the same values
πŸ‘‡ Python has the following da...
5️⃣ You can enable the ordering of instances by setting the order argument of the decorator to True.

It adds methods:
- __lt__
- __le__
- __gt__
- __ge__

They are implemented in the same way as __eq__
πŸ‘‡ A data class refers to a cl...
6️⃣ You can make instances immutable by setting the argument frozen to True

In such case dataclasses.FrozenInstanceError is raised if you try to reassign instance attribute The dataclass decorator is ...
7⃣ You can read more here.
docs.python.org/3/library/data…

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Akshay Kaushik πŸπŸ‘¨β€πŸ’»

Akshay Kaushik πŸπŸ‘¨β€πŸ’» Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @AkshaykKaushik

Feb 7, 2022
#Python 🐍 3.10: has a "Switch" like statement.

But it's not a switch, it's called "Pattern Matching."

Pattern matching is way beyond the "Switch Statement"
πŸ‘‡ python Pattern matching is like Switch Statement
The pattern matching takes a value following match and allows us to write out several potential cases, each defined by case. Where we find a match between the match-case we will execute the respective code. #programming πŸ‘‡
Even though it is not a "switch", it can be used as.πŸ‘‡
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(