🤔 Python data classes

What are they? How to use them?

🧵Let's find out👇
1️⃣ There's really nothing special about the class: the dataclass decorator adds generated methods
- __init__
- __repr__
- __eq__

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

For example, you can define User with fields id and name.👇
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
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

For example👇
5️⃣ You can enable order 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__
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
7️⃣ You can use __post_init__ hook on data classes to set the value of the attribute based on the others' values

• • •

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

Keep Current with Jan Giacomelli

Jan Giacomelli 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 @jangiacomelli

6 Jan
🤔 Python generators

What are they? How to use them?

#Python

🧵Let's find out 👇
1️⃣ Python generators are lazy iterators delivering the next value when their .next() is called.

They are created by using the yield keyword

next() can be called explicitly or implicitly inside for loop

They can be finite or infinite Image
2️⃣ yield - where a value is sent back to the caller, but the function doesn’t exit afterward as with the return statement

The state of function is remembered.

For example, the number is incremented and sent back from yield at the consecutive call next() Image
Read 9 tweets
29 Dec 20
Are you confused about pytest's fixtures?

Do you struggle to understand them?

#100daysOfCode #pytest #Python

🧵This one is for you👇
1️⃣ pytest's fixtures are functions decorated with "pytest.fixture" decorator

They can return value or produce side effects like creating/removing a database.

They can be located inside conftest.py or inside the test file.
2️⃣ To use the value returned from the fixture function inside a test you need to add a parameter with the same name as the fixture function.
Read 9 tweets
1 Dec 20
Don't know where and how to start with ML?

#100daysOfMLCode #100daysOfCode

🧵This is my journey so far👇
1️⃣ I started with ML during my master's degree.

I decided to try to build a model of my guitar amplifier.

I started with this course:

udemy.com/course/machine…
2️⃣ After I finished it I started playing around with Keras and scikit-learn to build a model of my guitar amplifier.

It took 72 hours of recording, 250+ hours of training, and 6 months of experimenting to conclude - ML algorithms like NN won't produce anything useful.
Read 12 tweets
30 Nov 20
☁️ AWS is a great choice for a startup

Here are some reasons why

#100daysOfCode #100daysOfMLCode #AWS

🧵👇
1️⃣ Free tier

Most of the services offer free tier for first 12 months since registration.

There are enough free resources per month that you're able to build MVP for almost free.
2️⃣ Designed for scalability

All of their services are designed with scalability in mind.

You can scale your startup business without limits.
Read 8 tweets
29 Nov 20
Don't know how to land your first tech job?

It's not easy but it's possible

🧵This is how I did it👇

#100daysOfCode #100daysOfMLCode
It was starting my master's at university in electrical engineering.

For some reason, I decided to try to get a job as a software engineer to increase my income.

I knew how to program in Matlab & C. It was enough to pass exams but not enough to build a project from scratch.
I decided to learn the basics of Javascript, PHP, and Laravel.

I built a couple of small and simple projects (e.g. web page for my band with Laravel).

I searched for open positions in my town. I didn't meet more than 25% of the requirements for any of them.
Read 8 tweets
24 Nov 20
🤷‍♂️You've learned basics of Python and you don't know what to do next?

How to go from "hello world" to real applications?

🧵 This one is for you👇

#100daysOfCode #100DaysOfMLCode #Python
1️⃣ Build expense tracker CLI app.

Each expense should have following attributes:
* title (string)
* amount (float)
* created_at (date)
* tags (list of strings)

Store expenses in TXT file.

Cover: Add expense, list expenses, get expense, edit expense, delete expense
2️⃣ Add database

Instead to storing/reading in/from TXT file start using SQLite.

Write script to copy all of the existing expenses from TXT file to database.

Don't use ORM at this point.
Read 14 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

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

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!