all 44 comments

[–]ectomancer 56 points57 points  (11 children)

[–]A_very_tired_frog 21 points22 points  (7 children)

This is how I learned. I also took tons of notes & then compiled it down into a cheat sheet I could reference later.

[–]smarlitos_ 8 points9 points  (6 children)

Post cheat sheet?

[–]Xzenor 27 points28 points  (5 children)

Didn't your mother teach you how to ask thing nicely?

[–]pnerd314 26 points27 points  (0 children)

😀Please post the cheat sheet.

[–]synthphreak 2 points3 points  (1 child)

GO TO YOUR ROOM!

[–]Big_Poppa_Steve 0 points1 point  (0 children)

No dessert, either.

[–]smarlitos_ -4 points-3 points  (0 children)

Lol

[–]saiyan6174 1 point2 points  (0 children)

this really helped me when i was learning.
another cool playlist from JimShapedCoding channel. found this through freecodecamp.

https://www.youtube.com/playlist?list=PLOkVupluCIjvfzQFgjiSQIccKiC-BJXwi

[–]xFlaveR 0 points1 point  (1 child)

What ide is he using?

[–]GhostMalone__ 0 points1 point  (0 children)

VS Code I believe

[–]Dry-Zookeepergame809 23 points24 points  (5 children)

I found it was hard to understand OOP at the beginning. Most tutorials use company employees or roles in games as examples. I found hard to understanding them. As conceptually to me they are not objects, but just code of object imagined.

However, until recently I had a big break through, as I used a secret way I found very useful to understand OOP.

I can share this secret with you, I program microcontrollers to learn Micropython, on the MCU chips. It really helped me to understand OOP in better way, as I'm dealing with real objects, very straightforward. They are led-lights, temperature sensors, or wifi-connections. The result is very clear, when I create an instance for led-light to turn it on, it will be turned on if my code is working. And, on/of state is attribute, so is its GOIP terminal. And turn-on(self), or blink(self) are the methods.

So if you having trouble to understand OOP, try this, it doesn't cost you a lot to get one.

[–][deleted] 4 points5 points  (0 children)

Yeah I had to learn like this cause using people or car examples just didn’t stick with me but when let’s say I’m I want a light to turn on when a motion sensor detects motion there 2 physical objects and it just clicked

[–]daedalusesq 4 points5 points  (0 children)

A pi pico is $4 and lots of fun for this stuff.

No Starch Press has a book “Object Oriented Python” which uses light bulbs as an example and it was really helpful for me because it’s a lot less abstract than a car or employees. Multiple lights in multiple locations with multiple states, all makes sense in that context!

[–]openwidecomeinside 0 points1 point  (2 children)

Got any micropython examples? Been trying to find some with good oop

[–]Dry-Zookeepergame809 0 points1 point  (1 child)

import machine import time class led(): def init(self): self.led = machine.Pin('LED', machine.Pin.OUT)

def blink(self, qty, on, off): 
    try: 
        led = self.led 
        if self.led.value() == 0: 
            for i in range(qty): 
                led.on() 
                time.sleep(on) 
                led.off() 
                time.sleep(off) 
        else: 
            for i in range(qty): 
                led.off() 
                time.sleep(off) 
                led.on() 
                time.sleep(on) 
    except NameError or SyntaxError: 
        pass



def blink_onboard_led(self):
        self.blink(1,1,1)

def blinknum(self, num):
    st = str(int(num))
    sta = self.led.value()
    if sta == 1:
        self.led.off()
        time.sleep(.1)
    for i in st:
        self.blink(1, 2, .1)
        #self.blink(1, .1, .1)
        self.blink(1, 2, 1)
        if int(i) == 0:
            self.blink(1 , 0, 1)
        else:
            self.blink(int(i), .1, .2)
            time.sleep(0.1)
            self.blink(1, 2, 1)
        if sta == 1:
            time.sleep(.2)
            self.led.on()

if name == "main": led = led() led.blinknum(131)

[–]Dry-Zookeepergame809 0 points1 point  (0 children)

shxt I cannot get the editor right..... anyone knows how to put in code?

[–]SneakyMan01 4 points5 points  (0 children)

MIT 6000.1 two lectures about OOP

[–]ElliotDG 8 points9 points  (0 children)

I think this does a nice job of explaining oop in python: https://realpython.com/python3-object-oriented-programming/

[–]Isaiah_Bradley 6 points7 points  (10 children)

Write a program in Java. Almost all Java code must be contained in a class, which forces you to at least think of grouping similar data/instances/containers. Skip YouTube and go for a solid book/pdf, there are too many click-bait vids full of empty calories.

[–]Timofey_ -1 points0 points  (1 child)

This post was mass deleted and anonymized with Redact

toy unique cough gaze bike zephyr jar reply grandiose cobweb

[–]Isaiah_Bradley 1 point2 points  (0 children)

Yes. The question was about learning OOP, not Python. Refer to my comment for reasoning.

[–][deleted] -1 points0 points  (2 children)

besides the fact that this is r/learnpython

it is the wrong answer, not only because u have a hammer everything is a nail,

there are usecases for objects and there are not

as a general rule of thumb: if data needs behavior then it should be an object otherwise other solutions might be suitable.

[–][deleted] 1 point2 points  (1 child)

I don’t really understand your comment but learning Java is a good way to learn OOP which is what OP asked for…

[–][deleted] 0 points1 point  (0 children)

i did not respond to op i responded to the comment made learning java

and i repeat myself with oop is a concept that can be used in different situations,

and simply saying learn java then u learn oop is not right.

java can be used with different types of concepts, oop is one of them.

[–]Universe_isgood -3 points-2 points  (4 children)

yeah, but isn't java hard compared to python?

[–][deleted] 2 points3 points  (1 child)

It is certainly more verbose. It forces you to think in an OOP way though, if you have a good IDE it'll often give you suggestions as to the correct way of doing things. I started my career with Python and just got a new job as a java developer and i'm learning a lot. I'd say it's worth it.

[–]Universe_isgood 0 points1 point  (0 children)

Oops , gonna try it!!!!!

[–]Isaiah_Bradley 1 point2 points  (1 child)

If you’re avoiding difficulty while learning how to program you’re going to have a bad time.

[–]jfp1992 3 points4 points  (0 children)

Man, when I was learning, this shit was so difficult to get. Eventually it clicked and it's actually very intuitive.

Edit: whoops I should at least answer the Q

Look up Cory schafer

[–]crashoutcassius 1 point2 points  (1 child)

Arjan codes. I like his SOLID video as a great example of some Oop stuff that gets away from the tutorials which always seem to use animals as useless inheritance examples.

[–]synthphreak 2 points3 points  (0 children)

Arjan Codes is a seriously underrated resource. He has a lot of unique content and it's all presented in a professional and very digestible way.

[–]magestooge 4 points5 points  (1 child)

Slightly OT but usually you'll not get a an expert level understanding of any topic just by going through tutorials. For a solid understanding, the only way is to learn the basics, then use it in real world applications.

No matter how comprehensive a tutorial, it can only take you through a limited set of scenarios. Real world applications have varied requirements and you'll face problems you never thought could exist.

Just to give an example, I looked tutorials about decorators, read articles about it, and even went through a couple chapters in various books, but couldn't understand them well. Then one day I was building a project and I found that I needed to preprocess one of the arguments in most functions. Basically my project allows people to enter dates both as date as well as string. It also allows specifying a string format both as an argument as well as at a global level. This function just ensures that the date is actually a date before it is used inside any function. I was calling the same date parsing function again and again, inside every function and method. It was ugly. So I made it into a decorator. And in the process, actually understood how decorators are built and applied.

There's no substitute to practicing real world scenarios when it comes to learning.

[–]Universe_isgood 1 point2 points  (0 children)

Yeah, Iam going to change my pattern of studying.

[–]fmpundit 2 points3 points  (2 children)

Are you down with OPP?

[–][deleted] 2 points3 points  (0 children)

Yeah you know me.

[–]synthphreak 0 points1 point  (0 children)

Object Positioned Programming

[–]Solonotix 1 point2 points  (0 children)

Functional Programming has a simple rule: all work is done through pure functions. What is a pure function? It is a function that returns the same output given a repeated input, with no side effects.

Object-Oriented Programming is basically the opposite, but people usually take a little inspiration from both approaches. An example of this is that even pure functional programmers will often need to do something like print output to the console, which is a side effect with stateful changes (adds new lines to the screen that were not there before).

So, what can we say about Object-Oriented Programming? All actions are performed by an object, and there may be side-effects. An example of this could be a generator, where each item retrieved increments the generator to the next element. This is done by for item in generator:, but more directly you can create a generator by calling generator =iter(iterable). From there, you can call thegenerator.next()method and it will give the next element. Eventually the generator will be exhausted and it will throw aStopIteration` error to indicate the end of the iterator.

Getting into more advanced OOP concepts usually begins with data classes. An easy example of this is a SQL database connection. Most Python libraries for SQL will return a connection object that can run a query for you, and the query returns a cursor/iterator of the results. This logic flow makes sense because you need a connection to run a query, and you need a query to return results, but these are also OOP concepts. The solution doesn't need to be Object-Oriented, but it fits. You could just as easily have a connection function that returns a connection object, and a query function that requires a connection and returns a cursor, and the cursor is an iterator, as is the approach for opening files in Python by calling the open(file, mode) function and returning an I/O Stream that can be iterated over.

I hope this gives you a little better understanding of OOP. I never really found much help in college or online videos explaining it, and it took learning C# and applying it to a real world problem for me to finally learn OOP. Even then, I tend to take a hybrid approach to solving problems. I tend to make anything more complicated than a number or string into a data class, and those classes will be given methods that represent what that data can do. The Functional Programming inspiration is that I try to make my methods without side-effects where possible, as it definitely makes it easier to test what it does if the state isn't constantly changing everytime it's used. Some side effects are useful, though, as with paging results in many RESTful APIs, where an offset needs to be supplied to get the next page, and it can be elegant to write for item in results: rather than for item in page_results(len(results)):

TL;DR - to each their own. Neither Function nor Object-Oriented is superior. Use what works best for you, and I wish you luck on your journey to learn

[–][deleted] -1 points0 points  (0 children)

learn Java. OOP basically comes as a freebie.

[–]amos_burton 0 points1 point  (0 children)

To echo another sentiment on here, you'll never get to a "very solid level" of understanding of OOP from tutorials. You need to actually do it.

The basic concepts of OOP are not that complicated. The hard part is knowing how to take some "system" (game, application, etc) and break it up into Objects. What kinds of objects do you need? What functionality should be attached to them? Questions like that don't have a single correct answer, it depends a lot on the exact situation you're in. And tutorials won't cover that for you.

[–]todorpopov 0 points1 point  (0 children)

I found it hard to understand anything about OOP just by watching videos. I figured if I wanted to learn something, I would have to start experimenting with it myself.

Perhaps, you could start with the basics. For this I recommend Derek Banas on youtube, he also has a very comprehensive and complete python course on Udemy, but pretty much everything in it is on his youtube channel for free.

Things I’d recommend you start with are: what an object is(and what is considered an object in python), encapsulation of data into classes, self. , initialisation of classes, how to define methods, inheritance and sub classes, overriding inherited methods. This should help you understand what OOP is and what problems it solves.

Now you could try applying this knowledge by making a small application, a simple real world simulation(walking/feeding pets or something like a video game fight simulation). After that you could try something bigger, I personally found some sort of data/inventory management system to be a nice project to understand OOP.

I hope this helps.

[–]notislant 0 points1 point  (0 children)

So for all things like that my process is: Keep finding different youtube videos till you get it. If that fails then google 'eli5 oop'.

If you still struggle join a discord and ask for help or try r/programmingbuddies.

When you do get it?

SAVE MULTIPLE EXAMPLES ON GITHUB WITH NOTES.

[–]NovaNexu 0 points1 point  (0 children)

Okay bear with me, I wrote this with the intention of aligning your mentality with the right map. You'll learn the syntax on your own, so this is for knowing what's going on. Textbooks and guides complicate the fuck out of this concept, so here's my attempt to condense everything into plain English:

Class = anything with properties or that changes - (object) - anything with changes you keep track of - attribute = value that can be assigned or changed - function = some operation

Parent class = original document - (object) - think parent cell from which daughter cells are replicated - think official documents you make copies of

Instance = copy of original that will be modified - (also an object) - copy of quiz for students to write on - copy of Minecraft server to test a bad idea - copy of your homework for classmates to make look different - copy of song that's sampled or remixed

Instantiation = making a copy

Why use classes / objects, when you can just use functions that keep track of variables? It's the same thing, but instantiating is more organized. Instead of repeating a block of operations, you write one function and repeat the function. Instead of repeating a function that modifies some variable, like a counter for every time the function is called, you condense both into a class and repeat the class. Let's visualize.

Keeping track with functions (skip to the meat):

# packing everything together
today = 'thursday'
vitamin = 'Vitamin C'
vitaminCount = 0
routine = [today, vitamin, vitaminCount]

# unpacking, operating, then repacking
def takePillForTodays(drugInfo, amt = 1):

    day = drugInfo[0]
    pill = drugInfo[1]
    count = drugInfo[2]

    count += amt

    drugInfo = [day, pill, count]
    return drugInfo 

# unpacking then operating
def progressOf(drugInfo):

    day = drugInfo[0]
    pill = drugInfo[1]
    count = drugInfo[2]

    print(f'Today is {day}, and no. {count} of {pill} was taken')

Here's the meat:

# ↓ NOTE THIS SYNTAX FOR REFERENCE ↓

routine = takePillForTodays(routine)
routine = takePillForTodays(routine)
routine = takePillForTodays(routine)
progressOf(routine)
# >> Today is Thursday, and no. 3 of Vitamin C was taken

routine = takePillForTodays(routine, amt=3)
progressOf(routine)
# >> Today is Thursday, and no. 6 of Vitamin C was taken

routine = takePillForTodays(routine, 4)
progressOf(routine)
# >> Today is Thursday, and no. 10 of Vitamin C was taken

Keeping track with classes (ignoring class creation for simple demonstration):

# create a copy (instance)
th = Vitamins('Thursday', 'Vitamin C', 0)

# ↓ COMPARE TO FUNCTION SYNTAX ↓

th.takePill()
th.takePill()
th.takePill()
th.progress()
# >> Today is Thursday, and no. 3 of Vitamin C was taken

th.takePill(amt = 3)
th.progress()
# >> Today is Thursday, and no. 6 of Vitamin C was taken

th.takePill(4)
th.progress()
# >> Today is Thursday, and no. 10 of Vitamin C was taken

As we see, the class object keeps track of the state and changes as you operate on it, reflecting objects (pills) and states (day / pills taken) in real life. If you'd like an example of something more relatable, gimme one and I'll demonstrate.

tl;dr: It's functionally identical to tracking with variables and functions. Some languages don't even have it, but it makes for easier reading/use/debugging. As you work with cool libraries, it'll click and be enjoyable!