If you talking about the game, I bought it on gog but never got the chance to install it. Too much time fighting with k8s manifests and no time for playing :c
You've got to read the books, a masterpiece. If you liked the TV show or didn't, it won't matter after reading just the first two short story collection.
One cool thing about the "frozen" attribute is that if set to True, Python will automatically create a ___hash___ function for your dataclass, allowing it to be used in things like dictionary keys and sets. (Note that if the class contains an unhashable field such as a list, the ___hash___ function will throw an exception).
When dealing with data in Python these days, I find myself almost exclusively doing comprehensions and functional programming with dictionaries and lists, rather than using classes. But good to know this is out there.
I started using classes when I could not do otherwise. My fonctions had like 15 variables that I had to state explicitly. It was impractical. My code was working on molecules and mixtures of molecules. So I created a class "mixture" and a class "molecule" and suddenly my fonctions were acting on mixtures and molecules rather than long lists of variables. That was such a relief. Now my classes have like 70 attributes because, well, mixtures and molecules have many structural features and properties. Everytime I extend my code I simply add new attributes and that's it.
This is something I was wondering to use in Python since I discovered that in PHP 8, you do not need to set the construct parameter to attributes anymore, you set the attributes directly on the parameter, this is very useful when you have a lot of initial parameters, pass the parameter and after that set it to an attribute is so so boring
8:43 "So frozen helps you to make sure the data is not changed anywhere in your code." ... right after bypassing this mechanism and showing how the data is changed in the code. :D Great videos, you are my no. 1 python instructor. Programming python itself is fun, but learning from a patient, calm, well explaining teacher is invaluable, but free. Thank you for sharing!!
Where is 'love' button? 'Like' is not an accurate reflection of what I feel about your videos. I've seen many amazing dudes with online tutorials on RU-vid, but you are the most amazing
Awesome! Coming from a C# background this really makes my day to know data classes are a thing in Python. Awesome. Really good video. Thanks Now I need to refactor all my custom data classes :-D
good video, for sort , dataclass will use all attributes by order for checking, so sort_index is not mandatory, its effected just because its the first one
I've been coding for... hmm... since python 2.5 as an amateur, when I need it. The vast majority of what I learned is from back then, and while i've been keeping learning, in the last years, besides some modules here and there, I've not learned many new things... discovered the chanel last week, and man I've learned so much since ! Sure there are few design pattern that I was already doing out of pure logic and commodity, though even then the knowledge acquired here have allowed me to refine these to the next level (or on the road to it), plus the terms that goes with it ! Thank you so much ! And there are many new neat things that I've been missing, like the module abstractclass or this dataclass. A great channel : advanced and intermediate concepts, in python !
Thank you Arjan! Your videos about how to make more of Python (with built-in functions), write better and cleaner code, etc. motivate me in trying to become more Pythonian.
Dude I'm a pretty experienced developer but brand new to python as of 2 months ago... I've seen like 5 videos on dataclasses by now and this is the first one that actually showed the benefits over regular classes... after by mile long init function is finished lol
Most of the programming channels show basic information with less relevant examples. I just came across this channel and let me tell you that you share the most relevant information I have ever seen on any channel. Thank you for your work, I really appreciate it!
All of your videos are still a little above my aptitude, I'm still relatively new to coding in general but I feel jumping in at the deep end can sometimes be good 👍
Another small thing: I see you using both single and double quotes. If you use a formatter (like Black) and set VSCode to Format on Save you’ll automatically get rid of these inconsistencies.
quick question about the __str__ representation you added, why did you use __str__ instead of __repr__? I usually choose __repr__ when it doesn't really make sense that an object be casted to a string but I still want to print it (for debugging or just to display it for some other reason) and I use __str__ when I think there is a relevant use case for a string casting. But these were my own conclusions and a convention I set for myself, is this a good practice? Is there any reason to use __repr__ over __str__ (or vice-versa)?
Hoi Arjan. Je maakt mooie video’s met duidelijke uitleg. Kun je een praktisch voorbeeld geven waarvoor jij data classes zou gebruiken. Ik zou denken dat, je die niet nodig hebt als je bv crud operaties wilt doen naar b.v. een SQL database
Hi Ronald, dankjewel! Dataclasses zijn erg handig in combinatie met Pydantic, aangezien je dan gebruik kunt maken van validatie van velden voor bijvoorbeeld APIs of database-modellen. Het is ook handig voor het representeren van een set config-waarden die je uit bijvoorbeeld een JSON file leest.
Such awesome content, this video got me to start learning python and I'm loving it! Big request though, could you do a video about Flask? Would be much appreciated!
@@ArjanCodes Can you compare Flask with FastAPI too? Seems like FastAPI is faster and is more pythonic in way, but I am unsure of the drawbacks of choosing FastAPI over Flask. Can you share your thoughts?
It's funny that the main touted advantage of Python is that it's got dynamically typed variables. And now the first thing the seasoned programmer does is *hammer those variables down to be a single type to avoid issues further down the road* 😁
What happens if, for example, self.sort_index = self.strength in __post_init__, we instantiate a Geralt at some point in the code but change his strength to something else later? Does sort_index somehow get updated automatically?
I love your presentation. Keep it up. Im much less impressed with Python. Yes its powerful, but in my opinion the language grows in a very non obvious way. It starts with the @ notation to introduce what would probably have been a pure language feature (as jn class/struct) had they thought of it originally. Then towards the end you had to add weird, less readable notation such as setting attributes to overcome the weakness of the original extention. To an extent this is typical of the modern "Agile" approach which encourages starting with trivial stuff, without doing to much conceptual design, and adding stuff as your requirements evolve. But Agile recognizes that there is a price to pay, technical debt, which it encourages you to fix. In the case of a language or infrastructure you cant do that, you're committed to backwards compatibility and similar issues, so the language structure slowly decays, becomes more complex and you lose consistency.. I'm interested in your take on these comments.
I do understand that you used sort_index to showcase field types, but wouldn't it be more intuitive to define sort_index to be a property? That way we ensure it's readonly and we also ensure the sort_index updates automatically™ as the relevant fields are updated.
Could that sort order field be made as a python property? Do we get some shortcuts around using slots for packing these in memory when in large arrays?
Very nice video, good explanations and well produced. When I see something like the frozen=True colliding with the __post_init then I get sad.... Coming from Java+lombok I am however, not really impressed with this dataclass annotation, but it did improve my knowledge of Python :) Keep the videos coming!
I've never been totally sold on data classes. To me, the functionality has been available forever in the form of dunder methods, @property decorators, and such. Am I being unreasonable here? Is there any sort of performance gain to data classes? Thanks for the excellent video as always!
Thanks! Actually, data classes are exactly the same as regular classes. The only thing the decorator does is already add dunder methods to make the class more suitable for dealing with representing data. So it's basically a shorter version of adding dunder methods yourself. Obviously, if what the data class decorator adds doesn't fit with what you need, then it is better to define those methods yourself, there is no particular performance gain to data classes.
This is a really cool feature and definitely useful for my work! Are data classes actually implemented differently at the c level or is this just a shorthand format for accessing commonly used features?
Hi Arjan, you have really great videos and great code. Can you do any video on database connection to python as OOP? Would love to see how you do it. Keep up the good work!
I tried making a playing card dataclass but since the comparison is dependent on the tuple of all values I had to override the comparison operators or "Ace of Hearts is > Ace of Diamonds (H > D)". I'm really new to Python (and programming), I thought dataclass would be good for playing card class as it's just a data holder anyway but if I ended up implementing my own sorting is it still worth using? Overhead? It's such a simple use case maybe I'm over complicating things? I think my only benefit was the repr, str, and the init having done for me.
@ArjanCodes I wanna ask a little. Is it ok if I can do like this @dataclass class Test: my_list = ["item1", "item2", "item3"] or there's a better approach for that?
It's not allowed to initialize a list directly like that with dataclasses. What you can do instead is rely on the __post_init__ method, and initialize the list there, as follows: @dataclass class Test: my_list: list[str] = field(init=False) def __post_init__(self): self.my_list = ["item1", "item2", "item3"] By adding the "field(init=False)" you don't have to provide a value for the list when you create an instance, so now you can do this: test = Test() print(test) # prints: Test(my_list=['item1', 'item2', 'item3'])
Do you think these are applicable to torch dataset subclasses? I have some horrendous dataset definitions that I’d love to refactor and I’m trying to find the cleanest way to rewrite them.
Maybe this is a dumb question, but because the sort index only gets set after initialization once. If the sort key is changed later(age is changed later in the program) then will it still sort correctly?
Seems useful and nicely Pythonic. Normally when folks say I should be writing classes, I tell them to go back to Java and leave us healthy people alone.