Aug-29-2020, 01:10 PM
The OOP parts of you questions are silly. There is no answer to how OOP is used in Python. I use OOP differently than another programmer because I see solutions differently and I work in a different problem space. I would never use classes the way you are using classes in your examples. That doesn't mean your thinking is wrong. OOP is a design tool that can be used many different ways.
If you want persistent objects you need to implement persistence. If you want a database of patients you should use a database. The code that uses the database can be designed using OOP principles or not. I don't think it matters. The database can be object oriented or not. I don't think that matters either.
Python garbage collection is based on reference counting. When you assign a Python object to a variable the reference count increases by 1. If you use the same variable to reference a different Python object the reference count decreases by one. When the reference count goes to zero the object is made available for garbage collection. The memory is reused to make other Python objects.
In your example with the two toms, the first object is de-referenced when you re-use the variable tom to reference a second object. The object's memory is made available to the heap for making new objects.
Python classes are really just wrappers around a dictionary with a dash of syntactic sugar. You can add attributes to a dictionary, so you can add attributes to a class. A class in C has attributes that are defined before an instance of that class is ever created. This is not true for Python. In Python only the methods and class variables are defined and each instance is free to have whatever attributes are assigned. Usually instances are assigned in the __init__ method, but they don't have to be. If you want all instances to have the same attributes and don't want the user to ever add attributes you can use __slots__ when you define your class.
If you want persistent objects you need to implement persistence. If you want a database of patients you should use a database. The code that uses the database can be designed using OOP principles or not. I don't think it matters. The database can be object oriented or not. I don't think that matters either.
Python garbage collection is based on reference counting. When you assign a Python object to a variable the reference count increases by 1. If you use the same variable to reference a different Python object the reference count decreases by one. When the reference count goes to zero the object is made available for garbage collection. The memory is reused to make other Python objects.
In your example with the two toms, the first object is de-referenced when you re-use the variable tom to reference a second object. The object's memory is made available to the heap for making new objects.
Python classes are really just wrappers around a dictionary with a dash of syntactic sugar. You can add attributes to a dictionary, so you can add attributes to a class. A class in C has attributes that are defined before an instance of that class is ever created. This is not true for Python. In Python only the methods and class variables are defined and each instance is free to have whatever attributes are assigned. Usually instances are assigned in the __init__ method, but they don't have to be. If you want all instances to have the same attributes and don't want the user to ever add attributes you can use __slots__ when you define your class.