Python Forum
MIT course - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: MIT course (/thread-2071.html)



MIT course - snippsat - Feb-16-2017

I was looking little at the new MIT course that been publish on Youtube.
It's been a while now since MIT switch from Java to Python.

Video from here,teaching about getters and setters.
A little further in the video,is strongly advice to always use getters and setters and not access data attribute directly.
I disagree with this,what is your's option on this?
No use string formatting in 2016/2017 all + Dodgy 

So if someone has not read this article from 2004 Python is not Java.
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.



RE: MIT course - ichabod801 - Feb-16-2017

When I was working in Java I didn't use them, because it was a pain and I was the only one using my code. I don't worry about it in Python either, unless there are dependencies between attributes that need to be handled (and sometimes I handle that other ways). First of all, I expect Python programmers to read the docstrings and use the attributes appropriately. Second of all, in my experience you can't stop people from messing up your code. As the saying goes, "if you make something idiot proof, the world will make a better idiot."


RE: MIT course - Larz60+ - Feb-16-2017

Quote:is strongly advice to always use getters and setters and not access data attribute directly.

I do use them when appropriate, but only then.

There have been several trends at MIT lately that have been unsettling.
I don't feel that this should become a rule, what would it serve.

I could jest (pun intended) as easily make a statement saying that you must use the subway everywhere you go.
That would certainly limit your range.

This sounds like a power thing to me, not a piratical one, probably by an ex Java programmer.

Bottom line. I don't hate them, but I don't see where they should be a rule either.

Snippsat, google this 'getters and setters in python'

You will not be happy, here's what comes up in the highlighted top box:


Quote:Getters and setters are used in many object oriented programming languages to ensure the principle of data encapsulation. They are known as mutator methods as well. ... Unfortunately, it is widespread belief that a proper Python class should encapsulate private attributes by using getters and setters.
It's actually against the use of them, but your eye doesn't immediately catch the 'Unfortunately' and it looks as though they are advertising their use.


RE: MIT course - snippsat - Feb-16-2017

Quote:You will not be happy, here's what comes up in the highlighted top box:
Yes i know Wink

So to a take a example that i did rewrite in a other forum.
The tutorial is from here.
Here is the code from tutorial:


I did a Rewrite of code over,no getters and setters.
To code is also shorten to 1/3:



RE: MIT course - micseydel - Feb-17-2017

(Feb-16-2017, 10:06 PM)snippsat Wrote: A little further in the video,is strongly advice to always use getters and setters and not access data attribute directly.
I disagree with this,what is your's option on this?
Sounds like a Java person teaching Python. Properties are exactly why it's not necessary in Python. You do it in Java because it's a pain to make the attributes private later and force users to update to a getter. The problem simply doesn't exist in Python. Scala doesn't have the problem either, Java just hasn't caught up with the times and this is a good example of it.


RE: MIT course - Ofnuts - Feb-17-2017

(Feb-16-2017, 10:54 PM)Larz60+ Wrote:
Quote:is strongly advice to always use getters and setters and not access data attribute directly.

I do use them when appropriate, but only then.

The second part of the phrase is right, but the first is not. Getters and setters aren't any better than accessing attributes directly. Data encapsulation isn't about changing the class attributes transparently, it is about object state integrity. If you have a Person class with name and surname attributes, you shouldn't be able to change these independently (which is what can still happen with setters), otherwise when you change John Doe to Jean Dupont there is a time window where the person is Jean Doe (or John Dupont). Person should have a rename(firstname,surname) method (possibly with locks...).