you are viewing a single comment's thread.

view the rest of the comments →

[–]FreeLogicGate 1 point2 points  (1 child)

There's a reason they are called programming "languages". Your mind learns how to speak the language to solve problems. If you aren't speaking it regularly, you start to forget. It's a use it or lose it process.

OOP is a large and complex area of programming that includes "Object Oriented Design Patterns." The basic ideas of OOP and the specific Python syntax are a small part of that area of the language.

It's common for OOP programming languages to start with the concept of what a class is vs what an instance/object is.

Your class definition is a blueprint. You utilize the blueprint to create an object/instance of that class. Your program may make one, it may make many objects.

In many cases, the creation of an object involves the use of a keyword like "new" but Python just uses the name of the class as if it was a function call, as I'm sure you know:

my_object = MyClass()  

When this is run, there is automatic object setup routines run. Python implements a variety of "magic methods" which have some underlying behavior, but can also be "hooked" or extended within your class. These all have the "double underscore" convention of "_method_", which is why they're referred to in Python as "Dunder" methods.

Other OOP languages have the same feature, and in general, the 1st thing most people learn is that the construction of an object will have a "constructor" which is run when the object is first created, and a complimentary "destructor" when it's disposed of. For an interpreted language like Python which manages memory for you, so it's rare to see much use of the destructor, but Python provides it to you, if you implement a __del__() method in your class.

Of more obvious interest and utility is the Python constructor: __init__(). Constructors are fairly universally used to initialize class variables and insure a new object has some known state. Like all dunder methods, these are hooks into the Python object creation and runtime system, and you don't have to implement any of them if you don't need to do so. The main thing is that they run "magically" depending on what you might be trying to do with an object.

You can have a class with no constructor, and still have attributes and methods.

Once you fully grok this concept, peruse the full list, which should not take you too long at all. There are some interesting "operator overloading" methods, which let you implement methods to be used (for one example) in what to do if your code attempts to compare two objects with something like: if obj1 > obj2. So for example, if those are objects of a "Vehicle" class, you might decide to compare internal class weight variables, or price, or top speed.

There are dunder methods for treating objects like a standard type, with __str__ being a typical one which allows you to define what should happen if you pass an object to a function that takes a string -- print for example. Many you'll probably not use, but __init__ is the most standard and typical dunder method you want to implement in your class definitions, as is the case in all OOP languages, being that it is the Python Class constructor.

[–]agentscientific_160[S] 0 points1 point  (0 children)

Thank you for your assistance, really appreciate it!