 Hello everyone. So just before we start, I'd like to thank the Euro Python organizers for this awesome event It's been like five years that we're sponsoring it and it's always a great pleasure. I Also want to thank Eric Smith the author of this pep for all his work on Python and Also for his unexpected answers to my emails. So thank you Eric So my name is Guillaume Gelin If you want to find me on the internet, I'm called Ramnes So if you want to add me on LinkedIn Facebook and if anything Ramnes that you use the place I'm working as a lead software engineer at Numboli. Numboli is a data marketing company We have a booth at the minister floor. So feel free to to come and see us As a lot of company around we are recruiting. So if you're searching for a job You're welcome Just a quick word before we really go into it. I started playing with data classes not so long ago So please don't take my word for everything I say I met as much as much research as I could to give you the best talk possible And the more research the more research I did on the subject and the more I wanted to add on this talk So it's a bit long. I'm not sure we'll have time for questions But if you have some feel free to come at the booth and to tweet me up All right, so it's going to be pretty fast. Are you ready? Yeah Okay, let's go So let's go back one week Before I was in vacations in the south of France Near the sea and that was great I'm I'm kind of like an unperturbed an entrepreneurial guy. So I was thinking what startup could I launch today? So the first thing you you look at when you're Searching for us to build a startup is a domain name, right? So I found this domain name, which is amazing.com. I Thought it's it's a good domain name. I bought it and And I thought I could make money out of it, right? so I want to my idea is to make a marketplace of of seller and buyers and To have that marketplace I need an inventory of all the times I have right So I have a friend who told me well Python tree is not great It's slower than Python 2 you should you should use Python 2 So it's been a long time that I didn't use Python and but I remember like this name-to-pole thing, right? So the first thing I did was to create some Items in my inventory with that name-to-pole And it was great. It did the job But as my cut base went larger and larger I ended up doing like not really DRI things Repeating a lot of stuff and and one thing really I know me is that I couldn't define defaults So I took a look at Python 3 and So yeah missing properties defaults I took a look at Python 3 and I discovered that new version of name-to-pole which you don't apart from Collections, but from typing and which has uppercase letters So it arrived in Python 3.5 And the annotation support that you can see here arrived in a Python 3.6 So it's it's it's way better because rather than Calculating every time my my total cost of all the items I have in my inventory. I can define that function once And then call it whenever I need And I have some kind of default too like I have a default entity which is zero But the problem is what happens if my mutable if my default is immutable like if it's a list for example Let's say I want to for my items. I want to have all the related items to that items So I want a list of items If I define it directly in the class just like I just like I did for quantity It's not going to work very well because each time I'm going to modify The list in an instance is going to be modified in all instances. So it's not that great So I thought like maybe I could like overload the new Dunder new method so that When I get my related items, I check if it if it's there or not if it's not there. I create an empty list And that's why I do my default But suddenly that version of native tuple doesn't follow that like It's weird because Python used to be much more permacive than that, right? so We're almost there, but Not yet so What I thought I could do because I don't like when people like tell me no you can't do that Is to define a real new method? To assign it to the new method and it works Well, it's a bit creepy, right? So yeah, nothing to see here. Please move on so This is the idea behind pay 557. It's to provide Real a real data class that you could use for that kind of stuff So it arrived in Python 3.7 It's available in Python 3.6 through a backboard called data classes, which is on github And that's great because you can take a look at the source code. So very interesting It's made by Eric Smith Which is the owner of a small IT business in in New York called true blade What they do is mostly written in Python Eric is a co-developer of CPython the author of several major contribution including STF format and f-strings He started to use Python a long time ago to escape C but ended up doing a lot of C to cut Python and He's happy because that a class is actually a place where it could code in the standard library in Python And actually use f-strings that he also could add himself So that's great So what Eric wanted is something that sounds and and feels like name tuples But with real real defaults and that can be mutable because name tuples are immutable, right? So he created data class. I'm pretty sure you've seen that example already So If you if you compare it to the the example with the name tuple, it's it's really close, right? There is the only difference is that you don't have the name tuple inheritance, but in place you have the data class decorator And the reason you choose the decorator is to avoid inheritance So that you don't have diamond problems and also decorators are a great way to inject methods inside a class And maybe this is something that we'll see more and more in the standard library Also data classes are meant to be used with type annotations So it's Python 3.7 and Python 3.6. So annotation at theirs are there to be used so think of it So what data class give you the decorator it injects? init, dndr init method and dndr init representation that there init string What the init gives you is that For each field that you've defined It will Expect them to be given at the instantiation time The representation is pretty good or so it's kind of moiconic like you can evaluate it itself Also, you have an explicit hash saying that like the explicit hash is hash equal none Saying that you can't hash a data class And it's and it has a lots of metadata like dndr data class fields that contains All the field names the types the default values the field options All the data classes you have another metadata which is dndr data class param That contains all the parameters that were used to generate the class, but we'll see that after So when I speak about real defaults. Oh, yes, it's almost propel your line So here for the rotate rotate items example So I've made I've used field which is something that you import from data classes and It gives you an option which is default factory which you can give like any function Here I just want to instantiate the list each time. I instantiate an item and I it's working just as expected as we can see So that's great There is that dndr post init method which is specific to to data classes Or not, maybe I'm not sure And it's great because you can Specify things like if we don't want the total cost to be appropriate for example We can compute it on the ones here and then just use it when we want What we only have to do is to define the field directly on top on the top The unit falls is to say that don't expect that field At in station instantiation time. I have a very hard time to pronounce this Just just expect it to be created later So for example in the post init you can freeze data classes. So you can pass frozen equal true And that does what you expect it to do which means that if you try to modify a field It's real. It's will raise an error be one. So Real real Freezing in Python is really hard There is like real acts to to change this so one hack is to is to use object dot then there's it that's here and This is actually useful because if you want a field to be computed at the instance instantiation time, but you don't want it to be modifiable be After you can do it that way and it's actually the recommended way by Eric so Feel free to use it Also, like I said by default Dunder ish equal none. So by default you can't use data classes as keys for dicks for example But if you really want to you can use the option unsafe ash And you can also inside fields define which field you want to be considered in the ash or not for example here I'm saying that related items is is not to consider into the ash because I think that Related items is a multiple list and it's maybe going to change very often So I don't want my ash to be fucked up. So I'm not going to take it into account I'm just going to consider name and unit price and So if I instantiate three three items two hammers with the same price and one spammer one spanner Then if I try to make a set out of it, we see that effectively we have two Two items and not three also So for those who don't know my pie my pie is an experimental optional static type checker for Python What it does it it analyzes the code without running it and uses annotations to detect when incorrect types are used So that a class works well with my pie That being say the the version of my pie is that and those data classes is not released yet So it will be released pretty soon, but it works And if for some reason you want to to go back to that name to pull way of Doing things you can there is a make that a class function the only difference with Collections that name to pull is that you can give type hints and you can also give defaults and fields options Just just like I showed before And it's basically the same as the name to pull behavior Also one great thing is that If you want to go back from your data classes to more standard types like dicks or tuples you have helpers for this So as dict and as tuples inside data classes allows you to do that The order is defined by the field definition orders So if you define the name first in your class and then the price in second Then you'll have that order in your dicks and tuples So that's pretty great, right We have exactly what we want It's full DRI We have properties everything we want that's that's really great, but Let's go back in time a second time Maybe what I showed you remembers you something Maybe you've seen already a library called 80 tiers or I don't know how to pronounce it But 80 tier or something So it's Python 2.7 and 3.4 plus library that you can install on PIP obviously It's made by in X for schlawak. That's maybe is here. Yeah Please give him a big hand. No, he's not well is in dear Python, but not in this room but The 80 tier library is really what The the the origin idea of data classes, right? So Eric when he wants when he created the data classes PIP He wants through a lot of males issues its draft 80 tiers To understand their decision-making what choice what trade-off did they took? And as you can see, it's really really close to data classes, right? You have that 80 tier dot s which is the equivalent of data classes You have that 80 tier dot ib which is which allows you to give a default So that's really really similar But it's much more powerful it gives you much more methods as you can see there is in its rep as their egg ash But you also have n e l t l e g t e g etc It's it has validators which are not present in data classes So for example here, I have a price And I don't want an item to be over nice thousand dollars So I can just validate that with the small snippet And it's used the decorators setters for that pretty pretty smart You also has built in well built in 80 tier validators like instance of which allows you to Dynamically verify the type of your of your field and not just with my pie or some static analyzer thing you also converters converters like for example if you're working with external APIs and You you know that the types on that external API are fucked up But you want in your database to have proper types Then you can use a convert the converter option of 80 tier dot ib Which means by the way at rib like attribute. I took one word to understand that So Right now you may be wondering like why I would use data classes if 80 tier is doing basically the same thing and it's much more powerful, right? And this is answered in the pep actually so the ID is 80 tier is a sort party library that want to keep its freedom to move fast And to implement any features I want to implement also Python is really oriented towards simplicity And a lot of features that are in 80 tier are not useful like 100% of the time So I don't know you but personally I tend to value a lot simplicity So if I don't need the extra features, I'm mostly going to use data classes And also I prefer data classes syntax, which is I feel much more clear because eight as we just shown before 80 tier syntax is a bit strange But hey, I guess it's just a matter of personal preference, right? So Let's go back to my wonderful amazing.com website What if I had a no SQL database behind right something like MongoDB the data class example would in the world because In a schemed database, you don't know the field in advance or you can't know them But you want to have the freedom of letting someone modifying it then directly in the database without Without it breaking all your code base So here we can't use databases data classes, right? So for example with MongoDB If we connect to the database and get documents inside a collection, we get a dictionary The problem with dictionaries is that it misses real object-oriented programming types So for example if I have users on the side and and items on the other side If both are dicts, how do I know which which one is what like do I just check the fields? like I'd prefer to be able to do like is instance of user or is instance of item you miss properties and also the dot notation because the dict notation where you have like braces everywhere is a bit cumbersome So one smart guy could make Could inherit from dict and just make the properties that would work That's that's a bit creepy also So we're not going to stay on this And there is a thing called simple namespaces It's in a module called types. So there is collections typing and types. It's not the same thing And that simple namespace class is is really neat. You can see it like a Bear a very bear version of data classes. It just give an in its method and and a representation the method to the class that inherits from it and What's great is that doesn't ask you to define fields. So in case of the MongoDB example, it's it's nice Because you can just take your whatever is your dict and just throw it inside your class and and and you have The the hopi type you have the dot notation and you have the properties But one thing that's missing here is that we can't go back to normal types we can't we can't We can't have a dict would have Simple namespace we can't have a table so you should end up doing that kind of tough Maybe we could have something more powerful There is that library called box Which is on github that you can install on people so which is basically a wrapper around dict It works a bit like simple namespace But you can you you have a method like to underscore dict that allows you to go back to a dict I think if I'm not wrong that there is also a to topple method. I'm not so sure about it, but maybe and The great thing about box also is that if you have dicts inside dicts inside dicts it's going to To take the whole thing and to make box would have everything so you can do it and one dot Item two dot item three it's true But the problem with box is that it's super slow I'm not really much into performance usually, but here it's Really problematic So as we as you can see here like Here the it's a micro benchmark Which is mostly about measuring the time it takes to create a bunch a bunch of object like 50,000 And also with some reads inside You can see that using a dict or a or a or a simple namespace is super fast Then you have You have I can't read my slide Name to polls like collection name to pulse on the non-typed one Name to the type at one is also great And singy I will talk about it later and then eight it here and And the data classes are pretty much at the same level of performance But box is like three time like you take three three month three times more Time to create a box than a than a data class. So that's not very nice So let me introduce you to singy singy is a library we have developed at Numbly It's Python 2.7 and 3.4 plus compatible it can you can install it on the pip it's on github It looks like simple namespaces But it has that view method Which allows you to go back to a dict And There's a really nice thing about singy is that you can have different kind of views you can define new views so for example If I want to be able to have In my dict the result of the property I've defined I can I can define a view Called with total for example and include the total the total cost inside And when I when I will call item view with total I'll get a dict with the property included You can also exclude fields Select exactly which fields you want that kind of stuff So for example when you're in the context of a REST API for example Backed by something with MongoDB. It's really nice because you can't say exactly for each of you of your API What field do you want to return? And by the way, we have we have Mongo thingy, which is basically a node EM object document mapper based on singy, which is something a bit like Mongo engine But but much more simple and much more close to PyMongo query language So it's Q&A time, but I'll start with a self Q&A so The reason I made this talk in the first place is because I was developing thingy and I saw the data class thing And I saw shit. Maybe what I'm doing for a year. It's completely easier is less Maybe they just implemented something and Python that's doing exactly the same thing But as we've just seen it's I think there is place for both because data class is more on on type it stuff and and really Define field that kind of stuff a thingy is much more about Freedom and and you can put whatever you want the inside So the answer is no Does that a class deprecates name tuples? Honestly? Yes, definitely Whatever modules are from Does it deprecate SQL alchemy? SQL alchemy is a big big thing. It's used by a lot of people. So I don't see it deprecated anywhere soon But I could imagine a rebase of SQL alchemy on data classes. I don't think it's going to be Soon, but it could happen Does it deprecate Marshmallow? So for those who don't know Marshmallow is a validation library where you can define class which with each field being like a rule to to That you feel must must comply too So once again, I don't think so But I think Marshmallow could rebase on data classes especially since data classes was sought with with Extendability in mind So it's it's really like you can plug so party libraries on it So maybe that's a good idea. I don't know and the last question Would be does it deprecate class? And I leave that cash that question to you. Thank you All right, so we have time for maybe one more question Okay, then I Suck at live questions, so please speak well Did you run more benchmarks and all the competitions because I can imagine that you would use data classes or address or name tuples if you have a lot of instances like database rows and then memory consumption may be Important to for example. Yeah, I didn't make more benchmarks I really did just made that benchmark because I Wanted to use in a real world example like in a real-world situation I wanted to use books and that's really what made me like retire your books So that's what I showed that benchmark, but Yeah, feel free to do more benchmarks Any other questions last one It could be fast as a result of philosophy The new thing that sometimes it's good to have a clear model because you said the Mongols pay to the scale But the moment you get too many fields so so actually No in the latest version of mongo you have you can enforce chemas directly inside the database, so It's it's but it's like the the database administrator walk. It's not your walk as a developer But yeah, yeah, definitely there are situations where you want clear schemas, right? And I'm not saying that that's not the case. I'm just saying that sometimes you don't want that Yeah, it's basically for a running and when your system becoming larger with a lot of parameters a lot of properties It's easy to make a mistake. So yeah, that's the trade-off. Thank you All right. Thank you One big round of applause