Python Forum
Is inheritnace the only option
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is inheritnace the only option
#4
(Oct-29-2017, 11:53 PM)QueenSvetlana Wrote: Why not use the getters/setter?

Python is not Java(2004)
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.

class Student():
    def __init__(self, id, first_name, last_name, type_of_student):
        self.id = id
        self.first_name = first_name
        self.last_name = last_name
        self.type_of_student = type_of_student

    def international_documents(self, documents):
        return documents
    
    def __str__(self):
        return f'ID: {self.id} First: {self.first_name} Last Name: {self.last_name} Type: {self.type_of_student}'
Use:
>>> Superman = Student(100, 'Clark', 'Kent', 'International')
>>> Superman.id
100
>>> Superman.last_name
'Kent'

>>> print(Superman)
ID: 100 First: Clark Last Name: Kent Type: International

>>> Superman.international_documents('born Kal-El on the planet Krypton')
'born Kal-El on the planet Krypton'
As you see there is no problem to access data attribute directly,no need for getters/setters.
So can have in information doc that International need to apply doc.
Can also make a check that International most apply doc.
Reply


Messages In This Thread
Is inheritnace the only option - by QueenSvetlana - Oct-29-2017, 10:43 PM
RE: Is inheritnace the only option - by metulburr - Oct-29-2017, 11:50 PM
RE: Is inheritnace the only option - by snippsat - Oct-30-2017, 05:51 PM
RE: Is inheritnace the only option - by metulburr - Oct-30-2017, 10:49 PM
RE: Is inheritnace the only option - by metulburr - Oct-31-2017, 01:41 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020