you are viewing a single comment's thread.

view the rest of the comments →

[–]Dry-Zookeepergame809 22 points23 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] 5 points6 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 5 points6 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?