The Best is the Enemy of the Good: Getters, Setters, and pathlib
The Best is the Enemy of the Good: Getters, Setters, and pathlib
After a week of not working late, I found myself late in the office processing some of my software engineering goals.
Hey, it's okay to get things wrong sometimes.
It's okay to make mistakes.
It's okay to set rules for yourself and to only stick to them 90% of the time.
Or 70% of the time.
I love coding.
I don't know how I ever got through without it. People search their whole lives for something like this.
I only waited 28 years...
Getters, Setters and Deleters
Using getter, setter and deleter methods are one way to implement encapsulation within Python.
This allows classes to handle the state of their own attributes.
Some methods are only intended to be used inside of their own class. But why? I asked chat gpt and apparently this is to keep things simpler, to avoid errors, and to keep things safer - to avoid problems - whatever on earth that means.
It allows you to control some kind of typing, I guess.
Had a chat with my mentor. It turns out that a lot of this is very very Java. Apparently in Python you might not even write get and set methods and you might just access the attributes directly as well anyway.
The main thing is
The main thing is, I went through the lesson, I understand lots and lots of bits now as well. My mentor explained to me why they are more Java than Pythonic. But I understand them as well and would know how to use them and recognise them which is good. I definitely understand them more than when I did them in JavaScript, well over a year ago now!
I don't know if I've understood everything perfectly but it's time to move on to the quiz. YAY!
Other random stuff I learned today
- When you write a test, you should also write a docstring saying what it does
- I need to consolidate this!!!
- read_text()
- read_text() is a method in Python that is used to read the contents of a file as a string
- It's part of the Path class in the pathlib module
- write_text()
- write_text() is also a convenient Python method, that is also a part of the Path class of the pathlib module
Comments
Post a Comment