Aug-29-2020, 12:04 PM
Hello all
As part of my journey to learn python I have slowly started getting to grips with Object Orientation.
I have 3 x questions which I was hoping someone could help me with.
1) How is OOP implemented in real life. For example I have a piece of code that creates patients, as shown below:-
I can create several different patient objects, however, when I turn my computer off and back on again the patients objects are lost and I have to re-create the same patient objects. My gut feeling is that the correct process would be that once an object is created then the data is stored in a dataframe / outputted to an excel document which is saved. When I turn off my computer and back on again Python reads in the excel document as objects – would this thinking be correct and is this how developers implement OOP in real life applications?
2) Using the example below, If I have a patient class from which I create a patient object such as tom = patient(23, “broken arm”), if I then entered the line of code tom = patient(45, “broken leg”) are there 2 x different objects created in memory i.e. tom 23 broken arm and tom 45 broken leg or is the first tom object overwritten? and would you find out how many objects have been created and stored in memory from a specific class?
3) Finally using the example below, my class only has 2 x attributes, Age and Illness. After creating an object in this case tom = Patient(23, “broken arm”), I can add other attributes such as hair_color for example tom.hair_color = "red", why is this allowed, it would seem correct and proper for all the attributes of an object to be defined in the class so what is the purpose of allowing attributes to be created in an object that are not part of the class?
Thank you.
As part of my journey to learn python I have slowly started getting to grips with Object Orientation.
I have 3 x questions which I was hoping someone could help me with.
1) How is OOP implemented in real life. For example I have a piece of code that creates patients, as shown below:-
1 2 3 4 5 6 |
class Patient(): def __init__( self , Age, Illness): self .Age = Age self .Illness = Illness print ( "Patient Object Created" ) |
2) Using the example below, If I have a patient class from which I create a patient object such as tom = patient(23, “broken arm”), if I then entered the line of code tom = patient(45, “broken leg”) are there 2 x different objects created in memory i.e. tom 23 broken arm and tom 45 broken leg or is the first tom object overwritten? and would you find out how many objects have been created and stored in memory from a specific class?
1 2 3 4 5 6 7 8 9 10 |
class Patient(): def __init__( self , Age, Illness): self .Age = Age self .Illness = Illness print ( "Patient Object Created" ) tom = Patient( 23 , "broken arm" ) tom = Patient( 45 , "broken leg" ) |
1 2 3 4 5 6 7 8 9 10 11 12 |
class Patient(): def __init__( self , Age, Illness): self .Age = Age self .Illness = Illness print ( "Patient Object Created" ) tom = Patient( 23 , "broken arm" ) tom.hair_color = "red" |