Python Forum
Better Understanding Of Object Orientation In Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better Understanding Of Object Orientation In Python
#2
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.
Reply


Messages In This Thread
RE: Better Understanding Of Object Orientation In Python - by deanhystad - Aug-29-2020, 01:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,372 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,836 May-09-2023, 08:47 AM
Last Post: buran
  Understanding Python classes PythonNewbee 3 1,245 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  python update binary object (override delivered Object properties) pierre38 4 1,830 May-19-2022, 07:52 AM
Last Post: pierre38
  Understanding Python super() for classes OmegaRed94 1 1,879 Jun-09-2021, 09:02 AM
Last Post: buran
  fpdf orientation not working properly KatMac 1 3,396 May-02-2021, 10:47 AM
Last Post: Pedroski55
  Understanding Python's Import Engine MysticaL 1 2,215 Feb-07-2020, 11:26 PM
Last Post: snippsat
  Help with understanding a python package pyhill00 4 3,098 Mar-21-2019, 12:42 AM
Last Post: Larz60+
  Understanding if Statements in Python Kathleen 1 2,468 Mar-05-2019, 07:55 PM
Last Post: Yoriz
  Understanding Scoping in Python yksingh1097 5 3,934 Aug-06-2018, 07:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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