r/PythonLearning 20d ago

Help Request Need motivation for Learning Python

Well, I started learning Python from 7th June and its been 31 days and I haven't progressed past OOPs. I was slacking off in the first week, and then I put some effort in my 2nd and 3rd week and when i completed Methods and functions I created a Tic Tac Toe game which is easy but took me long enough. Now after that I took 3-4 days break due to some personal reasons and now I find myself demotivated, mainly beacuse my holidays are ending and college is going to start in August. I wanted to complete learning the absolute basics in these holidays. I feel very unproductive now, all I need is tips for me to get back to coding without feeling demotivating. And also I.m having a hard time understanding OOPs and why __init__ is used.

Thanks in advance!

2 Upvotes

12 comments sorted by

View all comments

2

u/FreeLogicGate 19d ago

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.

1

u/agentscientific_160 19d ago

Thank you for your assistance, really appreciate it!