add-info function
Hii,
I have a problem with understanding In[121]:
steve.add_info(''Patient treated for ear infection- amoxycilin prescribed')
why we didn't write as:
steve.conditions.append(''Patient treated for ear infection- amoxycilin prescribed')
Hey Asmaa,
Thank you for reaching out!
There are several reasons to prefer steve.add_info(...)
over steve.conditions.append(...)
.
Firstly, the add_info()
method ensures that the additions to the conditions
list are managed through a consistent and controlled process. This restricts the operations that can be performed on an object and prevents external code (code outside the definition of the Patient
class) from disrupting the class's internal workings. This principle of object-oriented programming (OOP) is called 'encapsulation'.
Secondly, the user typically doesn't need to know the internal implementation of the class. By employing add_info()
, we abstract away the details, such as the conditions
attribute being a list. This OOP principle is called 'abstraction'. Abstraction allows users to interact with objects without needing to understand their complex inner workings.
Thirdly, implementing a method such as add_info()
provides enhanced flexibility and control. It can be customized to perform checks on appended values, log changes, trigger notifications when new conditions are added, etc.
Finally, and equally important, a method such as add_info()
is intuitive and self-explanatory regarding its purpose. Directly interacting with the conditions
list might be less clear and could potentially lead to confusion or misuse.
Hope this answers your question!
Kind regards,
365 Hristina